Some descriptive statistics for the dataset.
# load final dataset for Hungary
nests.per.week <- read.csv("../Data/Hungary_numofnests.csv")
# check loaded dataset
head(nests.per.week)
## X week year sites num.of.nests X.x pmean prec.lag1 prec.1.week
## 1 1 11 1991 2 1 171 0.90000000 0.0000000 0.01428571
## 2 2 12 1991 2 1 172 2.61428571 0.9000000 0.45000000
## 3 3 13 1991 2 2 173 0.00000000 2.6142857 1.75714286
## 4 4 13 1992 2 1 226 0.02857143 0.1571429 0.10000000
## 5 5 13 1993 2 2 279 0.78571429 3.0571429 1.70714286
## 6 8 14 1991 2 6 174 0.14285714 0.0000000 1.30714286
## prec.2.week prec.3.week prec.4.week prec.5.week prec.6.week X.y
## 1 0.08571429 0.06428571 0.3114286 0.6666667 0.5918367 171
## 2 0.30952381 0.28928571 0.2314286 0.4095238 0.7000000 172
## 3 1.17142857 0.88571429 0.7542857 0.6285714 0.7244898 173
## 4 0.06666667 0.05000000 0.1285714 0.2761905 0.2367347 226
## 5 1.13809524 1.33571429 1.2057143 1.0452381 0.8959184 279
## 6 1.17142857 0.87857143 0.7085714 0.6285714 0.5387755 174
## tmean temp.lag1 temp.1.week temp.2.week temp.3.week temp.4.week
## 1 10.150000 7.871429 6.328571 4.742857 3.053571 1.787143
## 2 9.185714 10.150000 9.010714 7.602381 6.094643 4.472857
## 3 9.178571 9.185714 9.667857 9.069048 7.998214 6.712857
## 4 10.078571 10.771429 7.371429 6.471429 6.614286 6.081429
## 5 3.564286 9.421429 8.650000 5.988095 4.157143 2.738571
## 6 10.900000 9.178571 9.182143 9.504762 9.096429 8.234286
## temp.5.week temp.6.week
## 1 0.5511905 -0.2479592
## 2 3.1809524 1.9224490
## 3 5.2583333 4.0387755
## 4 5.0452381 5.2948980
## 5 2.1797619 1.8459184
## 6 7.1238095 5.8183673
# check dimensions of the dataset
dim(nests.per.week)
## [1] 56 23
# check median number of nests per week
median(nests.per.week$num.of.nests)
## [1] 2
# check standard deviation of number of nests per week
sd(nests.per.week$num.of.nests)
## [1] 2.471592
# check range of number of nests per week
range(nests.per.week$num.of.nests)
## [1] 1 11
Some preliminary anaylysis before modelling to check for model assumptions and standardization of predictor variables.
# check for outliers
boxplot(nests.per.week$num.of.nests ~ nests.per.week$year, xlab = "Year",
ylab = "Number of nests per week", main = "Number of nests per week
for each year (Hungary)")
# check for homogeneitry of variances
with(nests.per.week, tapply(num.of.nests, year, FUN = var)) # violated by 1989, 1994
## 1989 1990 1991 1992 1993 1994
## 0.000000 8.809524 6.910256 9.072727 4.833333 1.694444
table(nests.per.week$year)
##
## 1989 1990 1991 1992 1993 1994
## 3 7 13 11 13 9
# check for normality and zero inflation
hist(nests.per.week$num.of.nests, breaks = 10) # no zero inflation
# standardize predictor variables
nests.per.week$z.temp.1.week <- scale(nests.per.week$temp.1.week)
nests.per.week$z.prec.1.week <- scale(nests.per.week$prec.1.week)
nests.per.week$z.temp.2.week <- scale(nests.per.week$temp.2.week)
nests.per.week$z.prec.2.week <- scale(nests.per.week$prec.2.week)
nests.per.week$z.temp.3.week <- scale(nests.per.week$temp.3.week)
nests.per.week$z.prec.3.week <- scale(nests.per.week$prec.3.week)
nests.per.week$z.temp.4.week <- scale(nests.per.week$temp.4.week)
nests.per.week$z.prec.4.week <- scale(nests.per.week$prec.4.week)
nests.per.week$z.temp.5.week <- scale(nests.per.week$temp.5.week)
nests.per.week$z.prec.5.week <- scale(nests.per.week$prec.5.week)
nests.per.week$z.temp.6.week <- scale(nests.per.week$temp.6.week)
nests.per.week$z.prec.6.week <- scale(nests.per.week$prec.6.week)
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.1.week", "prec.1.week")]
# plot the relationships
library(psych) # load required package : pysch
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.1.week + nests.per.week$prec.1.week)
# visually inspect relationships by each year
library(ggplot2) # load required package : ggplot2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (1 week) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot number of nests per week vs pre-laying period temperature
qplot(temp.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (1 week) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
library(mgcv) # load required package : mgcv
## Loading required package: nlme
## This is mgcv 1.8-24. For overview type 'help("mgcv-package")'.
M1 <- gam(num.of.nests ~ s(temp.1.week) + s(prec.1.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M1) # seems to have curvature
# build a maximal poisson model
library(lme4) # load required package : lme4
## Loading required package: Matrix
##
## Attaching package: 'lme4'
## The following object is masked from 'package:nlme':
##
## lmList
mod1.1 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ I(z.prec.1.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## I(z.prec.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 242.1 256.3 -114.1 228.1 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1206 -0.6771 -0.2314 0.5705 2.1440
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1130 0.3361
## year (Intercept) 0.0154 0.1241
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.25238 0.17094 7.326 2.37e-13 ***
## z.temp.1.week -0.19323 0.10919 -1.770 0.0768 .
## z.prec.1.week -0.11643 0.12880 -0.904 0.3660
## I(z.temp.1.week^2) -0.28517 0.13636 -2.091 0.0365 *
## I(z.prec.1.week^2) 0.07861 0.04857 1.619 0.1055
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1. I(z.t.1.^2)
## z.temp.1.wk -0.139
## z.prec.1.wk 0.311 -0.033
## I(z.t.1.^2) -0.688 0.301 -0.124
## I(z.p.1.^2) -0.444 0.037 -0.761 0.198
# remove quadratic term for precipitation
mod1.2 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod1.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 242.7 254.9 -115.4 230.7 50
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1317 -0.6839 -0.2651 0.6317 2.2991
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.12303 0.3508
## year (Intercept) 0.03064 0.1750
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.34763 0.16084 8.379 <2e-16 ***
## z.temp.1.week -0.19048 0.10617 -1.794 0.0728 .
## z.prec.1.week 0.03795 0.08985 0.422 0.6727
## I(z.temp.1.week^2) -0.31512 0.12988 -2.426 0.0153 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1.
## z.temp.1.wk -0.070
## z.prec.1.wk -0.019 -0.045
## I(z.t.1.^2) -0.621 0.209 -0.020
# remove precipitation term
mod1.3 <- glmer(num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod1.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 240.9 251.0 -115.5 230.9 51
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1091 -0.6693 -0.2461 0.6463 2.2032
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1299 0.3603
## year (Intercept) 0.0256 0.1600
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.3477 0.1589 8.480 <2e-16 ***
## z.temp.1.week -0.1886 0.1062 -1.776 0.0757 .
## I(z.temp.1.week^2) -0.3138 0.1312 -2.393 0.0167 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1.
## z.temp.1.wk -0.071
## I(z.t.1.^2) -0.636 0.207
# remove quadratic term for temperature
mod1.4 <- glmer(num.of.nests ~ z.temp.1.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.1.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 245.0 253.1 -118.5 237.0 52
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0076 -0.6533 -0.2057 0.5290 1.3802
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.2149 0.4636
## year (Intercept) 0.0000 0.0000
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0530 0.1071 9.828 <2e-16 ***
## z.temp.1.week -0.1591 0.1025 -1.552 0.121
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.1.wk 0.096
# compare models using AIC
anova(mod1.1, mod1.2, mod1.3, mod1.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod1.4: num.of.nests ~ z.temp.1.week + (1 | year/week)
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod1.2: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.2: (1 | year/week)
## mod1.1: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.1: I(z.prec.1.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.4 4 244.97 253.07 -118.48 236.97
## mod1.3 5 240.90 251.03 -115.45 230.90 6.0689 1 0.01376 *
## mod1.2 6 242.72 254.88 -115.36 230.72 0.1762 1 0.67466
## mod1.1 7 242.14 256.32 -114.07 228.14 2.5814 1 0.10813
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.2.week", "prec.2.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.2.week + nests.per.week$prec.2.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (2 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (2 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M2 <- gam(num.of.nests ~ s(temp.2.week) + s(prec.2.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M2) # no curvature
# build a maximal poisson model
mod2.1 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ I(z.prec.2.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.0200477
## (tol = 0.001, component 1)
summary(mod2.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## I(z.prec.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 241.4 255.5 -113.7 227.4 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2553 -0.6556 -0.2505 0.6245 2.0501
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.09782 0.3128
## year (Intercept) 0.03415 0.1848
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.304731 0.003534 369.17 <2e-16 ***
## z.temp.2.week -0.178416 0.003527 -50.58 <2e-16 ***
## z.prec.2.week -0.155876 0.003527 -44.20 <2e-16 ***
## I(z.temp.2.week^2) -0.363290 0.003533 -102.83 <2e-16 ***
## I(z.prec.2.week^2) 0.091673 0.003519 26.05 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2. I(z.t.2.^2)
## z.temp.2.wk 0.000
## z.prec.2.wk 0.000 0.000
## I(z.t.2.^2) 0.001 0.000 0.000
## I(z.p.2.^2) 0.000 0.000 -0.003 0.001
## convergence code: 0
## Model failed to converge with max|grad| = 0.0200477 (tol = 0.001, component 1)
# remove quadratic precipitation term
mod2.2 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 241.7 253.8 -114.8 229.7 50
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1566 -0.6467 -0.2862 0.5231 2.2901
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.11829 0.3439
## year (Intercept) 0.03353 0.1831
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.39663 0.16345 8.545 < 2e-16 ***
## z.temp.2.week -0.16865 0.10524 -1.603 0.10903
## z.prec.2.week -0.02187 0.09482 -0.231 0.81761
## I(z.temp.2.week^2) -0.36877 0.13410 -2.750 0.00596 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2.
## z.temp.2.wk -0.052
## z.prec.2.wk -0.092 -0.097
## I(z.t.2.^2) -0.629 0.147 0.121
# remove precipitation
mod2.3 <- glmer(num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 239.7 249.8 -114.9 229.7 51
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1528 -0.6575 -0.3059 0.5143 2.3386
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.11747 0.3427
## year (Intercept) 0.03472 0.1863
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.3929 0.1633 8.527 < 2e-16 ***
## z.temp.2.week -0.1711 0.1049 -1.631 0.10292
## I(z.temp.2.week^2) -0.3651 0.1332 -2.741 0.00613 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2.
## z.temp.2.wk -0.063
## I(z.t.2.^2) -0.622 0.163
# remove quadratic term
mod2.4 <- glmer(num.of.nests ~ z.temp.2.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod2.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.2.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 245.4 253.5 -118.7 237.4 52
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0094 -0.6544 -0.2475 0.5376 1.4111
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.2183 0.4672
## year (Intercept) 0.0000 0.0000
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0533 0.1074 9.808 <2e-16 ***
## z.temp.2.week -0.1446 0.1030 -1.403 0.16
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.2.wk 0.086
# compare models using AIC
anova(mod2.1, mod2.2, mod2.3, mod2.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod2.4: num.of.nests ~ z.temp.2.week + (1 | year/week)
## mod2.3: num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | year/week)
## mod2.2: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.2: (1 | year/week)
## mod2.1: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.1: I(z.prec.2.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod2.4 4 245.40 253.50 -118.70 237.40
## mod2.3 5 239.72 249.84 -114.86 229.72 7.6808 1 0.005581 **
## mod2.2 6 241.66 253.82 -114.83 229.66 0.0533 1 0.817394
## mod2.1 7 241.36 255.54 -113.68 227.36 2.3003 1 0.129351
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.3.week", "prec.3.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.3.week + nests.per.week$prec.3.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (3 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (3 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M3 <- gam(num.of.nests ~ s(temp.3.week) + s(prec.3.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M3) # some curvature
# build a maximal poisson model
mod3.1 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ I(z.prec.3.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod3.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## I(z.prec.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 239.9 254.1 -112.9 225.9 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1759 -0.6214 -0.2134 0.5732 1.8035
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.09759 0.3124
## year (Intercept) 0.02234 0.1495
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.23987 0.16008 7.745 9.54e-15 ***
## z.temp.3.week -0.15799 0.10380 -1.522 0.12798
## z.prec.3.week -0.22077 0.12662 -1.744 0.08123 .
## I(z.temp.3.week^2) -0.32328 0.12437 -2.599 0.00934 **
## I(z.prec.3.week^2) 0.12224 0.05608 2.180 0.02928 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3. I(z.t.3.^2)
## z.temp.3.wk -0.065
## z.prec.3.wk 0.093 -0.061
## I(z.t.3.^2) -0.567 0.162 0.308
## I(z.p.3.^2) -0.351 0.027 -0.684 -0.084
# remove quadratic term for precipitation
mod3.2 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod3.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 242.3 254.5 -115.2 230.3 50
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1161 -0.6225 -0.2690 0.4440 1.8738
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1424 0.3774
## year (Intercept) 0.0163 0.1277
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.34649 0.14981 8.988 <2e-16 ***
## z.temp.3.week -0.16644 0.10604 -1.570 0.1165
## z.prec.3.week -0.04648 0.09723 -0.478 0.6326
## I(z.temp.3.week^2) -0.31253 0.12670 -2.467 0.0136 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3.
## z.temp.3.wk -0.007
## z.prec.3.wk -0.163 -0.114
## I(z.t.3.^2) -0.637 0.095 0.258
# remove precipitaion
mod3.3 <- glmer(num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 240.6 250.7 -115.3 230.6 51
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1153 -0.6356 -0.2713 0.4953 1.9641
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.14376 0.3792
## year (Intercept) 0.01498 0.1224
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.3338 0.1475 9.044 <2e-16 ***
## z.temp.3.week -0.1725 0.1063 -1.623 0.1045
## I(z.temp.3.week^2) -0.2977 0.1241 -2.399 0.0164 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3.
## z.temp.3.wk -0.036
## I(z.t.3.^2) -0.629 0.152
# remove quadratic temperature term
mod3.4 <- glmer(num.of.nests ~ z.temp.3.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.3.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 245.3 253.4 -118.6 237.3 52
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0394 -0.6560 -0.2462 0.5630 1.3750
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 2.161e-01 4.649e-01
## year (Intercept) 1.079e-10 1.039e-05
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0538 0.1072 9.832 <2e-16 ***
## z.temp.3.week -0.1483 0.1028 -1.443 0.149
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.3.wk 0.086
# compare models using AIC
anova(mod3.1, mod3.2, mod3.3, mod3.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod3.4: num.of.nests ~ z.temp.3.week + (1 | year/week)
## mod3.3: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.2: (1 | year/week)
## mod3.1: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.1: I(z.prec.3.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod3.4 4 245.29 253.40 -118.65 237.29
## mod3.3 5 240.55 250.68 -115.28 230.55 6.7410 1 0.009422 **
## mod3.2 6 242.32 254.47 -115.16 230.32 0.2314 1 0.630466
## mod3.1 7 239.90 254.07 -112.95 225.90 4.4277 1 0.035361 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.4.week", "prec.4.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.4.week + nests.per.week$prec.4.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (4 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (4 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M4 <- gam(num.of.nests ~ s(temp.4.week) + s(prec.4.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M4) # seems to have curvature
# build a maximal poisson model
mod4.1 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ I(z.prec.4.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod4.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## I(z.prec.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 238.3 252.4 -112.1 224.3 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1915 -0.5821 -0.2730 0.5945 1.8976
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.08674 0.2945
## year (Intercept) 0.02693 0.1641
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.20678 0.15590 7.741 9.87e-15 ***
## z.temp.4.week -0.16067 0.10607 -1.515 0.12982
## z.prec.4.week -0.21550 0.13199 -1.633 0.10254
## I(z.temp.4.week^2) -0.30570 0.11421 -2.677 0.00744 **
## I(z.prec.4.week^2) 0.13865 0.05572 2.488 0.01284 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4. I(z.t.4.^2)
## z.temp.4.wk -0.094
## z.prec.4.wk 0.123 -0.083
## I(z.t.4.^2) -0.515 0.205 0.304
## I(z.p.4.^2) -0.359 0.039 -0.740 -0.105
# remove quadratic precipitation term
mod4.2 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 242.1 254.2 -115.0 230.1 50
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1633 -0.6234 -0.2134 0.5358 2.0068
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.13697 0.3701
## year (Intercept) 0.02002 0.1415
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.32435 0.14565 9.093 <2e-16 ***
## z.temp.4.week -0.17787 0.10848 -1.640 0.1011
## z.prec.4.week 0.01163 0.09623 0.121 0.9038
## I(z.temp.4.week^2) -0.29195 0.11698 -2.496 0.0126 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4.
## z.temp.4.wk -0.034
## z.prec.4.wk -0.185 -0.142
## I(z.t.4.^2) -0.583 0.146 0.251
# remove precipitation
mod4.3 <- glmer(num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 240.1 250.2 -115.0 230.1 51
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1641 -0.6218 -0.2057 0.5194 1.9850
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.13757 0.3709
## year (Intercept) 0.01998 0.1413
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.3275 0.1432 9.272 < 2e-16 ***
## z.temp.4.week -0.1760 0.1072 -1.642 0.10067
## I(z.temp.4.week^2) -0.2956 0.1132 -2.612 0.00901 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4.
## z.temp.4.wk -0.060
## I(z.t.4.^2) -0.565 0.184
# remove quadratic term
mod4.4 <- glmer(num.of.nests ~ z.temp.4.week
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.4.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 245.8 253.9 -118.9 237.8 52
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0152 -0.6833 -0.2693 0.5806 1.3689
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.2207 0.4698
## year (Intercept) 0.0000 0.0000
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0540 0.1076 9.80 <2e-16 ***
## z.temp.4.week -0.1291 0.1033 -1.25 0.211
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.4.wk 0.075
# compare models using AIC
anova(mod4.1, mod4.2, mod4.3, mod4.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod4.4: num.of.nests ~ z.temp.4.week + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod4.2: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.2: (1 | year/week)
## mod4.1: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.1: I(z.prec.4.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod4.4 4 245.80 253.91 -118.90 237.80
## mod4.3 5 240.09 250.22 -115.05 230.09 7.7118 1 0.005486 **
## mod4.2 6 242.08 254.23 -115.04 230.08 0.0146 1 0.903892
## mod4.1 7 238.27 252.44 -112.13 224.27 5.8102 1 0.015934 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.5.week", "prec.5.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.5.week + nests.per.week$prec.5.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (5 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (5 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M5 <- gam(num.of.nests ~ s(temp.5.week) + s(prec.5.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M5)
# build a maximal poisson model
mod5.1 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ I(z.prec.5.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod5.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## I(z.prec.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 240.0 254.2 -113.0 226.0 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2549 -0.5811 -0.2127 0.5686 1.6840
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.09654 0.3107
## year (Intercept) 0.03513 0.1874
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.2349 0.1629 7.580 3.46e-14 ***
## z.temp.5.week -0.1535 0.1085 -1.415 0.15721
## z.prec.5.week -0.2040 0.1485 -1.373 0.16963
## I(z.temp.5.week^2) -0.3297 0.1117 -2.951 0.00317 **
## I(z.prec.5.week^2) 0.1242 0.0720 1.725 0.08460 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5. I(z.t.5.^2)
## z.temp.5.wk -0.076
## z.prec.5.wk 0.195 -0.116
## I(z.t.5.^2) -0.440 0.175 0.313
## I(z.p.5.^2) -0.423 0.024 -0.749 -0.108
# remove quadratic precipitation term
mod5.2 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 240.9 253.1 -114.5 228.9 50
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2486 -0.6263 -0.1559 0.5069 2.0020
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.12590 0.3548
## year (Intercept) 0.02737 0.1654
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.34617 0.14511 9.277 < 2e-16 ***
## z.temp.5.week -0.16498 0.11023 -1.497 0.13447
## z.prec.5.week -0.02343 0.10161 -0.231 0.81766
## I(z.temp.5.week^2) -0.32222 0.11112 -2.900 0.00373 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5.
## z.temp.5.wk -0.044
## z.prec.5.wk -0.174 -0.190
## I(z.t.5.^2) -0.530 0.132 0.297
# remove precipitation
mod5.3 <- glmer(num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 239.0 249.1 -114.5 229.0 51
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2394 -0.6260 -0.1666 0.5097 2.0477
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.12657 0.3558
## year (Intercept) 0.02612 0.1616
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.3401 0.1421 9.430 < 2e-16 ***
## z.temp.5.week -0.1699 0.1085 -1.566 0.11735
## I(z.temp.5.week^2) -0.3148 0.1064 -2.958 0.00309 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5.
## z.temp.5.wk -0.082
## I(z.t.5.^2) -0.510 0.210
# remove quadratic term
mod5.4 <- glmer(num.of.nests ~ z.temp.5.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod5.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.5.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 246.4 254.5 -119.2 238.4 52
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -0.9721 -0.6997 -0.2826 0.5925 1.3880
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.2266 0.476
## year (Intercept) 0.0000 0.000
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.0540 0.1080 9.755 <2e-16 ***
## z.temp.5.week -0.1024 0.1042 -0.983 0.326
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.5.wk 0.061
# compare models using AIC
anova(mod5.1, mod5.2, mod5.3, mod5.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod5.4: num.of.nests ~ z.temp.5.week + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod5.2: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.2: (1 | year/week)
## mod5.1: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.1: I(z.prec.5.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod5.4 4 246.39 254.49 -119.19 238.39
## mod5.3 5 239.00 249.12 -114.50 229.00 9.3913 1 0.00218 **
## mod5.2 6 240.94 253.10 -114.47 228.94 0.0536 1 0.81686
## mod5.1 7 240.01 254.18 -113.00 226.01 2.9390 1 0.08647 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.6.week", "prec.6.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.6.week + nests.per.week$prec.6.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (6 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (6 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M6 <- gam(num.of.nests ~ s(temp.6.week) + s(prec.6.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M6) # only temperature seems to have curvature
# build a maximal poisson model
mod6.1 <- glmer(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ I(z.prec.6.week^2) + (1|year/week), family = "poisson" ,
data= nests.per.week)
summary(mod6.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## I(z.prec.6.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 237.5 251.7 -111.7 223.5 49
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2769 -0.6115 -0.1559 0.5545 1.7760
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.07335 0.2708
## year (Intercept) 0.06308 0.2512
## Number of obs: 56, groups: week:year, 56; year, 6
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.17622 0.18322 6.420 1.36e-10 ***
## z.temp.6.week -0.16701 0.10937 -1.527 0.126747
## z.prec.6.week -0.34805 0.17621 -1.975 0.048241 *
## I(z.temp.6.week^2) -0.35113 0.10635 -3.302 0.000961 ***
## I(z.prec.6.week^2) 0.19564 0.09252 2.115 0.034463 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6. z.p.6. I(z.t.6.^2)
## z.temp.6.wk -0.066
## z.prec.6.wk 0.317 -0.105
## I(z.t.6.^2) -0.332 0.205 0.310
## I(z.p.6.^2) -0.487 -0.026 -0.810 -0.149
# run a null model
mod6.2 <- glmer(num.of.nests ~ 1 + (1|year/week), family = "poisson" ,
data= nests.per.week)
# compare models using anova
anova(mod6.1, mod6.2, test = "Chi")
## Data: nests.per.week
## Models:
## mod6.2: num.of.nests ~ 1 + (1 | year/week)
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.1: I(z.prec.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod6.2 3 245.35 251.43 -119.67 239.35
## mod6.1 7 237.48 251.66 -111.74 223.48 15.869 4 0.0032 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Compare all the models to choose the best model. Also compare final model with a model without random effects to check for their importance. Check for model overdispersion.
# compare best models from different analyses
anova(mod2.3, mod1.3, mod3.1, mod3.3, mod4.1, mod4.3, mod5.3, mod6.1, test = "Chi")
## Data: nests.per.week
## Models:
## mod2.3: num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | year/week)
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod3.3: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod3.1: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.1: I(z.prec.3.week^2) + (1 | year/week)
## mod4.1: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.1: I(z.prec.4.week^2) + (1 | year/week)
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.1: I(z.prec.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod2.3 5 239.72 249.84 -114.86 229.72
## mod1.3 5 240.90 251.03 -115.45 230.90 0.0000 0 1.0000
## mod3.3 5 240.55 250.68 -115.28 230.55 0.3463 0 <2e-16 ***
## mod4.3 5 240.09 250.22 -115.05 230.09 0.4622 0 <2e-16 ***
## mod5.3 5 239.00 249.12 -114.50 229.00 1.0940 0 <2e-16 ***
## mod3.1 7 239.90 254.07 -112.95 225.90 3.1030 2 0.2119
## mod4.1 7 238.27 252.44 -112.13 224.27 1.6278 0 <2e-16 ***
## mod6.1 7 237.48 251.66 -111.74 223.48 0.7876 0 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# comparing best model with and without random effects
# run a model without random effects
M <- glm(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ I(z.prec.6.week^2), family = "poisson" , data= nests.per.week)
# compare the two models
anova(mod6.1, M, test = "Chi")
## Data: nests.per.week
## Models:
## M: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## M: I(z.prec.6.week^2)
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.1: I(z.prec.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## M 5 238.65 248.78 -114.32 228.65
## mod6.1 7 237.48 251.66 -111.74 223.48 5.1689 2 0.07544 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# check if the model is overdispersed
library(blmeco) # load required package : blmeco
## Loading required package: MASS
dispersion_glmer(mod6.1) # not overdispersed
## [1] 0.9316918
Plot model results for the best model
library(sjPlot) # load required package :sjPlot
# install package strengejacke for sjPlot to work properly
devtools::install_github("strengejacke/strengejacke", force = TRUE)
## Downloading GitHub repo strengejacke/strengejacke@master
## from URL https://api.github.com/repos/strengejacke/strengejacke/zipball/master
## Installing strengejacke
## '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file \
## --no-environ --no-save --no-restore --quiet CMD INSTALL \
## '/private/var/folders/_g/g0tfx7tn3qv6f6qtzkppkxd00000gn/T/RtmpVKY5fh/devtools2bc0b6637b/strengejacke-strengejacke-d41d521' \
## --library='/Library/Frameworks/R.framework/Versions/3.5/Resources/library' \
## --install-tests
##
# plot for effect of precipitation
p1 <- plot_model(mod6.1, type = "pred", terms = "z.prec.6.week",
axis.title = c("Pre-laying precipitation average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "blue")
# plot for effect of temperature
p2 <- plot_model(mod6.1, type = "pred", terms = "z.temp.6.week",
axis.title = c("Pre-laying temperature average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "red")
# arrange the plots together
library(gridExtra) # load required package : gridExtra
grid.arrange(p1, p2, nrow = 1, ncol = 2)
####### Temperature #######
# plotting pre-laying temperature by years on the same plot
nests.per.week$year <- as.factor(nests.per.week$year)
p <- ggplot(nests.per.week, aes(x=week, y=temp.6.week, col = year, group=year))
p + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average temperature for laying period by year and save as data frame
tmean <- as.data.frame(with(nests.per.week, tapply(temp.6.week, year, FUN = mean)))
# convert data frame's row names as the first column
tmean <- tibble::rownames_to_column(tmean, "year")
# check data frame
head(tmean)
## year with(nests.per.week, tapply(temp.6.week, year, FUN = mean))
## 1 1989 15.823810
## 2 1990 14.468950
## 3 1991 8.236421
## 4 1992 10.799072
## 5 1993 11.300000
## 6 1994 10.240816
# change name of second column
colnames(tmean)[2] <- "tmean"
# check data frame again
head(tmean)
## year tmean
## 1 1989 15.823810
## 2 1990 14.468950
## 3 1991 8.236421
## 4 1992 10.799072
## 5 1993 11.300000
## 6 1994 10.240816
####### Precipitation ########
# plot pre-laying period precipitation
P <- ggplot(nests.per.week, aes(x=week, y=prec.6.week, col = year, group=year))
P + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average precipitation for laying period by year and save as data frame
pmean <- as.data.frame(with(nests.per.week,
tapply(prec.6.week, year, FUN = mean, na.rm = T)))
# convert data frame's row names as the first column
pmean <- tibble::rownames_to_column(pmean, "year")
# check data frame
head(pmean)
## year
## 1 1989
## 2 1990
## 3 1991
## 4 1992
## 5 1993
## 6 1994
## with(nests.per.week, tapply(prec.6.week, year, FUN = mean, na.rm = T))
## 1 2.525170
## 2 1.191545
## 3 1.878022
## 4 0.429128
## 5 1.092779
## 6 1.275057
# change name of second column
colnames(pmean)[2] <- "pmean"
# check data frame again
head(pmean)
## year pmean
## 1 1989 2.525170
## 2 1990 1.191545
## 3 1991 1.878022
## 4 1992 0.429128
## 5 1993 1.092779
## 6 1994 1.275057
# plots for climatic trends at Hungary
# set page layout to incorporate two plots side by side
par(mfrow = c(1,2))
# set the axis lables to be slightly bigger
par(cex.lab=1.4)
# plot annual mean temperature for the laying period vs year
plot(tmean$tmean ~ tmean$year,
main="Annual breeding season mean temperature (Hungary)",
ylab="Temperature (Degree celsius)", xlab="Year",
pch = 18, cex = 2, col = "red", cex.main = 1)
# plot annual mean precipitation for the laying period vs year
plot(pmean$pmean ~ pmean$year,
main="Annual breeding season mean precipitation (Hungary)",
ylab="Precipitation (mm)", xlab="Year",
pch = 18, cex = 2, col = "blue", cex.main = 1)
###### correlation test for climate data #######
# convert year to numeric
tmean$year <- as.numeric(tmean$year)
# test for correlation using spearman's correlation test
cor.test(tmean$tmean, tmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: tmean$tmean and tmean$year
## S = 56, p-value = 0.2417
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.6
# repeat for precipitation data
# convert year to numeric
pmean$year <- as.numeric(pmean$year)
# test for correlation using spearman's correlation test
cor.test(pmean$pmean, pmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: pmean$pmean and pmean$year
## S = 52, p-value = 0.3556
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.4857143
Repeat whole process for the population at Galicia.
Some descriptive statistics for the dataset.
# load final dataset for Hungary
nests.per.week <- read.csv("../Data/Galicia_numofnests.csv")
# check loaded dataset
head(nests.per.week)
## X week year num.of.nests X.x pmean prec.lag1 prec.1.week prec.2.week
## 1 1 12 1998 2 18 1.4000000 0.0000000 0.0000000 0.0000000
## 2 2 12 2002 2 231 0.1285714 1.0571429 3.3714286 2.7000000
## 3 3 12 2008 2 549 0.0000000 9.2000000 4.6000000 3.0666667
## 4 4 12 2009 1 602 1.6000000 0.0000000 0.0000000 0.3190476
## 5 5 13 1998 1 19 1.6285714 1.4000000 0.7000000 0.4666667
## 6 6 13 2001 1 179 0.0000000 0.5142857 0.3285714 0.2761905
## prec.3.week prec.4.week prec.5.week prec.6.week X.y tmean temp.lag1
## 1 0.0000000 0.0000000 0.1785714 1.1938776 13 17.17857 15.88571
## 2 2.4928571 1.9942857 1.6666667 1.4714286 226 16.74286 17.50714
## 3 2.3000000 3.3514286 2.8904762 2.5142857 544 14.17143 13.60000
## 4 0.3392857 0.2714286 0.2261905 1.0183673 597 16.37143 16.85714
## 5 0.3500000 0.2800000 0.2333333 0.3530612 14 15.75000 17.17857
## 6 1.1285714 3.0942857 2.5880952 2.2183673 174 16.71429 15.12143
## temp.1.week temp.2.week temp.3.week temp.4.week temp.5.week temp.6.week
## 1 15.58571 15.67619 14.92500 14.87000 14.87381 14.73265
## 2 15.70714 14.70000 14.08214 13.89714 13.81071 13.80918
## 3 15.16429 14.94762 15.20000 15.07714 14.87619 14.75918
## 4 16.68571 15.60476 15.02143 14.31714 13.92619 13.42449
## 5 16.53214 16.11667 16.05179 15.37571 15.25476 15.20306
## 6 15.47143 15.29524 15.52143 15.00571 14.58571 14.49388
# check dimensions of the dataset
dim(nests.per.week)
## [1] 204 22
# check median number of nests per week
median(nests.per.week$num.of.nests)
## [1] 2
# check standard deviation of number of nests per week
sd(nests.per.week$num.of.nests)
## [1] 1.617086
# check range of number of nests per week
range(nests.per.week$num.of.nests)
## [1] 1 11
Some preliminary anaylysis before modelling to check for model assumptions and standardization of predictor variables.
# check for outliers
boxplot(nests.per.week$num.of.nests ~ nests.per.week$year, xlab = "Year",
ylab = "Number of nests per week", main = "Number of nests per week
for each year (Galicia)")
# check for homogeneitry of variances
with(nests.per.week, tapply(num.of.nests, year, FUN = var)) # violated by 2017
## 1998 1999 2001 2002 2003 2004 2005
## 1.3076923 2.2637363 1.4102564 2.0000000 2.7500000 2.5151515 2.1813187
## 2006 2007 2008 2009 2010 2011 2013
## 1.8974359 1.1868132 6.2666667 8.9102564 2.9523810 0.4555556 0.2142857
## 2016 2017
## 0.7636364 0.9333333
table(nests.per.week$year)
##
## 1998 1999 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013 2016
## 13 14 13 16 12 12 14 13 14 16 13 15 10 8 11
## 2017
## 10
# check for normality and zero inflation
hist(nests.per.week$num.of.nests, breaks = 30) # no zero inflation
# standardize predictor variables
nests.per.week$z.temp.1.week <- scale(nests.per.week$temp.1.week)
nests.per.week$z.prec.1.week <- scale(nests.per.week$prec.1.week)
nests.per.week$z.temp.2.week <- scale(nests.per.week$temp.2.week)
nests.per.week$z.prec.2.week <- scale(nests.per.week$prec.2.week)
nests.per.week$z.temp.3.week <- scale(nests.per.week$temp.3.week)
nests.per.week$z.prec.3.week <- scale(nests.per.week$prec.3.week)
nests.per.week$z.temp.4.week <- scale(nests.per.week$temp.4.week)
nests.per.week$z.prec.4.week <- scale(nests.per.week$prec.4.week)
nests.per.week$z.temp.5.week <- scale(nests.per.week$temp.5.week)
nests.per.week$z.prec.5.week <- scale(nests.per.week$prec.5.week)
nests.per.week$z.temp.6.week <- scale(nests.per.week$temp.6.week)
nests.per.week$z.prec.6.week <- scale(nests.per.week$prec.6.week)
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.1.week", "prec.1.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.1.week + nests.per.week$prec.1.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (1 week) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot number of nests per week vs pre-laying period temperature
qplot(temp.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (1 week) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M1 <- gam(num.of.nests ~ s(temp.1.week) + s(prec.1.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M1) # seems to have curvature
# build a maximal poisson model
mod1.1 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ I(z.prec.1.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## I(z.prec.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 726.1 749.3 -356.0 712.1 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0849 -0.7570 -0.2427 0.5102 4.3445
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01104 0.10505
## year (Intercept) 0.00830 0.09111
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.98277 0.08015 12.262 <2e-16 ***
## z.temp.1.week -0.01012 0.05997 -0.169 0.8660
## z.prec.1.week 0.16241 0.09684 1.677 0.0935 .
## I(z.temp.1.week^2) -0.08395 0.05061 -1.659 0.0972 .
## I(z.prec.1.week^2) -0.04924 0.03202 -1.538 0.1241
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1. I(z.t.1.^2)
## z.temp.1.wk 0.346
## z.prec.1.wk 0.357 0.510
## I(z.t.1.^2) -0.607 -0.307 -0.094
## I(z.p.1.^2) -0.412 -0.365 -0.846 0.073
# remove quadratic term for precipitation
mod1.2 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod1.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 726.5 746.4 -357.2 714.5 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0259 -0.7689 -0.2482 0.4132 4.5158
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01493 0.1222
## year (Intercept) 0.01024 0.1012
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.92591 0.07407 12.500 <2e-16 ***
## z.temp.1.week -0.04542 0.05572 -0.815 0.415
## z.prec.1.week 0.03096 0.04938 0.627 0.531
## I(z.temp.1.week^2) -0.07739 0.05064 -1.528 0.126
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1.
## z.temp.1.wk 0.226
## z.prec.1.wk 0.005 0.391
## I(z.t.1.^2) -0.627 -0.301 -0.050
# remove precipitation term
mod1.3 <- glmer(num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod1.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 724.9 741.5 -357.4 714.9 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0108 -0.7631 -0.2602 0.4176 4.5206
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01683 0.12974
## year (Intercept) 0.00928 0.09633
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.92477 0.07381 12.529 <2e-16 ***
## z.temp.1.week -0.05886 0.05135 -1.146 0.252
## I(z.temp.1.week^2) -0.07581 0.05060 -1.498 0.134
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1.
## z.temp.1.wk 0.244
## I(z.t.1.^2) -0.631 -0.304
# remove quadratic term for temperature
mod1.4 <- glmer(num.of.nests ~ z.temp.1.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.1.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 725.2 738.4 -358.6 717.2 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -0.9904 -0.7873 -0.2377 0.4635 4.4079
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.022158 0.1489
## year (Intercept) 0.008208 0.0906
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.85053 0.05686 14.959 <2e-16 ***
## z.temp.1.week -0.08263 0.04764 -1.735 0.0828 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.1.wk 0.072
# compare models using AIC
anova(mod1.1, mod1.2, mod1.3, mod1.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod1.4: num.of.nests ~ z.temp.1.week + (1 | year/week)
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod1.2: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.2: (1 | year/week)
## mod1.1: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.1: I(z.prec.1.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.4 4 725.17 738.44 -358.58 717.17
## mod1.3 5 724.88 741.47 -357.44 714.88 2.2820 1 0.1309
## mod1.2 6 726.50 746.41 -357.25 714.50 0.3844 1 0.5353
## mod1.1 7 726.05 749.28 -356.03 712.05 2.4494 1 0.1176
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.2.week", "prec.2.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.2.week + nests.per.week$prec.2.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (2 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (2 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M2 <- gam(num.of.nests ~ s(temp.2.week) + s(prec.2.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M2) # curvature only for precipitation
# build a maximal poisson model
mod2.1 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ I(z.prec.2.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod2.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## I(z.prec.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 724.7 747.9 -355.4 710.7 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1637 -0.7313 -0.2304 0.5039 4.5675
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.007221 0.08498
## year (Intercept) 0.009219 0.09601
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.977768 0.082255 11.887 <2e-16 ***
## z.temp.2.week -0.008038 0.062985 -0.128 0.8985
## z.prec.2.week 0.193854 0.097250 1.993 0.0462 *
## I(z.temp.2.week^2) -0.056146 0.049983 -1.123 0.2613
## I(z.prec.2.week^2) -0.072366 0.039356 -1.839 0.0659 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2. I(z.t.2.^2)
## z.temp.2.wk 0.384
## z.prec.2.wk 0.342 0.567
## I(z.t.2.^2) -0.571 -0.285 0.008
## I(z.p.2.^2) -0.453 -0.442 -0.832 0.018
# remove quadratic temperature term
mod2.2 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.prec.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.prec.2.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 724.0 743.9 -356.0 712.0 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1460 -0.7307 -0.2522 0.5420 4.5114
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.009688 0.09843
## year (Intercept) 0.008150 0.09028
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.92256 0.06731 13.705 <2e-16 ***
## z.temp.2.week -0.02842 0.05951 -0.478 0.6329
## z.prec.2.week 0.19481 0.09773 1.993 0.0462 *
## I(z.prec.2.week^2) -0.07166 0.03963 -1.808 0.0706 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2.
## z.temp.2.wk 0.292
## z.prec.2.wk 0.429 0.602
## I(z.p.2.^2) -0.547 -0.464 -0.833
# remove temperature
mod2.3 <- glmer(num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 722.2 738.8 -356.1 712.2 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1339 -0.7346 -0.2374 0.5244 4.5302
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.010464 0.10229
## year (Intercept) 0.007905 0.08891
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.93149 0.06430 14.487 < 2e-16 ***
## z.prec.2.week 0.22298 0.07788 2.863 0.00419 **
## I(z.prec.2.week^2) -0.08047 0.03515 -2.290 0.02204 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.p.2.
## z.prec.2.wk 0.329
## I(z.p.2.^2) -0.486 -0.779
# remove quadratic term
mod2.4 <- glmer(num.of.nests ~ z.prec.2.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod2.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.prec.2.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 725.6 738.9 -358.8 717.6 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0755 -0.7748 -0.2132 0.4497 4.5685
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01973 0.1405
## year (Intercept) 0.01232 0.1110
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.84819 0.05913 14.345 <2e-16 ***
## z.prec.2.week 0.07688 0.04736 1.623 0.105
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.prec.2.wk -0.103
# compare models using AIC
anova(mod2.1, mod2.2, mod2.3, mod2.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod2.4: num.of.nests ~ z.prec.2.week + (1 | year/week)
## mod2.3: num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2) + (1 | year/week)
## mod2.2: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.prec.2.week^2) +
## mod2.2: (1 | year/week)
## mod2.1: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.1: I(z.prec.2.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod2.4 4 725.59 738.86 -358.80 717.59
## mod2.3 5 722.22 738.81 -356.11 712.22 5.3754 1 0.02042 *
## mod2.2 6 723.99 743.90 -355.99 711.99 0.2277 1 0.63321
## mod2.1 7 724.71 747.93 -355.35 710.71 1.2817 1 0.25758
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.3.week", "prec.3.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.3.week + nests.per.week$prec.3.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (3 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (3 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M3 <- gam(num.of.nests ~ s(temp.3.week) + s(prec.3.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M3) # curvature only for temperature
# build a maximal poisson model
mod3.1 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ I(z.prec.3.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod3.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## I(z.prec.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 726.9 750.1 -356.5 712.9 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0510 -0.7589 -0.2544 0.4159 4.7289
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.007494 0.08657
## year (Intercept) 0.015332 0.12382
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.89456 0.08305 10.771 <2e-16 ***
## z.temp.3.week -0.07842 0.06333 -1.238 0.216
## z.prec.3.week -0.01052 0.09234 -0.114 0.909
## I(z.temp.3.week^2) -0.07025 0.04908 -1.431 0.152
## I(z.prec.3.week^2) 0.02148 0.04052 0.530 0.596
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3. I(z.t.3.^2)
## z.temp.3.wk 0.357
## z.prec.3.wk 0.313 0.559
## I(z.t.3.^2) -0.469 -0.208 0.148
## I(z.p.3.^2) -0.435 -0.417 -0.814 -0.136
# remove quadratic term for precipitation
mod3.2 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod3.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 725.2 745.1 -356.6 713.2 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0346 -0.7778 -0.2610 0.4247 4.6591
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01007 0.1003
## year (Intercept) 0.01343 0.1159
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.91328 0.07418 12.312 <2e-16 ***
## z.temp.3.week -0.06441 0.05768 -1.117 0.264
## z.prec.3.week 0.02895 0.05410 0.535 0.593
## I(z.temp.3.week^2) -0.06681 0.04879 -1.369 0.171
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3.
## z.temp.3.wk 0.217
## z.prec.3.wk -0.083 0.414
## I(z.t.3.^2) -0.600 -0.296 0.068
# remove precipitaion
mod3.3 <- glmer(num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 723.5 740.1 -356.7 713.5 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0332 -0.7590 -0.2454 0.3997 4.5832
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.01224 0.1106
## year (Intercept) 0.01176 0.1084
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.91589 0.07341 12.476 <2e-16 ***
## z.temp.3.week -0.07711 0.05259 -1.466 0.143
## I(z.temp.3.week^2) -0.06859 0.04879 -1.406 0.160
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3.
## z.temp.3.wk 0.279
## I(z.t.3.^2) -0.604 -0.355
# remove quadratic temperature term
mod3.4 <- glmer(num.of.nests ~ z.temp.3.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.3.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 723.5 736.7 -357.7 715.5 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0874 -0.7594 -0.2749 0.4868 4.3938
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.017318 0.13160
## year (Intercept) 0.009718 0.09858
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.84952 0.05755 14.76 <2e-16 ***
## z.temp.3.week -0.10386 0.04787 -2.17 0.03 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.3.wk 0.092
# compare models using AIC
anova(mod3.1, mod3.2, mod3.3, mod3.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod3.4: num.of.nests ~ z.temp.3.week + (1 | year/week)
## mod3.3: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.2: (1 | year/week)
## mod3.1: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.1: I(z.prec.3.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod3.4 4 723.47 736.74 -357.74 715.47
## mod3.3 5 723.47 740.06 -356.73 713.47 2.0051 1 0.1568
## mod3.2 6 725.18 745.09 -356.59 713.18 0.2849 1 0.5935
## mod3.1 7 726.91 750.13 -356.45 712.91 0.2766 1 0.5990
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.4.week", "prec.4.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.4.week + nests.per.week$prec.4.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (4 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (4 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M4 <- gam(num.of.nests ~ s(temp.4.week) + s(prec.4.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M4) # some curvature
# build a maximal poisson model
mod4.1 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ I(z.prec.4.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod4.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## I(z.prec.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 725.8 749.0 -355.9 711.8 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0930 -0.7662 -0.2619 0.3867 4.6449
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.009584 0.0979
## year (Intercept) 0.011899 0.1091
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.97103 0.08176 11.876 <2e-16 ***
## z.temp.4.week -0.02628 0.06408 -0.410 0.6817
## z.prec.4.week 0.05995 0.08912 0.673 0.5012
## I(z.temp.4.week^2) -0.09163 0.04864 -1.884 0.0596 .
## I(z.prec.4.week^2) -0.03302 0.04743 -0.696 0.4862
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4. I(z.t.4.^2)
## z.temp.4.wk 0.364
## z.prec.4.wk 0.319 0.557
## I(z.t.4.^2) -0.423 -0.181 0.194
## I(z.p.4.^2) -0.473 -0.426 -0.782 -0.175
# remove quadratic precipitation term
mod4.2 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 724.3 744.2 -356.1 712.3 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0762 -0.7717 -0.2616 0.3594 4.6894
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.008861 0.09413
## year (Intercept) 0.013828 0.11759
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.94323 0.07280 12.956 <2e-16 ***
## z.temp.4.week -0.04530 0.05800 -0.781 0.4348
## z.prec.4.week 0.01083 0.05493 0.197 0.8437
## I(z.temp.4.week^2) -0.09768 0.04787 -2.041 0.0413 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4.
## z.temp.4.wk 0.201
## z.prec.4.wk -0.088 0.401
## I(z.t.4.^2) -0.577 -0.286 0.088
# remove precipitation
mod4.3 <- glmer(num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 722.3 738.9 -356.2 712.3 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0726 -0.7640 -0.2542 0.3632 4.6720
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.009432 0.09712
## year (Intercept) 0.013212 0.11494
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.94440 0.07230 13.061 <2e-16 ***
## z.temp.4.week -0.04987 0.05316 -0.938 0.3482
## I(z.temp.4.week^2) -0.09852 0.04771 -2.065 0.0389 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4.
## z.temp.4.wk 0.261
## I(z.t.4.^2) -0.576 -0.352
# remove quadratic term
mod4.4 <- glmer(num.of.nests ~ z.temp.4.week
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.4.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 724.7 737.9 -358.3 716.7 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0693 -0.7749 -0.2518 0.4834 4.4071
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.020602 0.14353
## year (Intercept) 0.009182 0.09582
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.84975 0.05737 14.812 <2e-16 ***
## z.temp.4.week -0.08968 0.04792 -1.872 0.0613 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.4.wk 0.083
# compare models using AIC
anova(mod4.1, mod4.2, mod4.3, mod4.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod4.4: num.of.nests ~ z.temp.4.week + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod4.2: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.2: (1 | year/week)
## mod4.1: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.1: I(z.prec.4.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod4.4 4 724.67 737.94 -358.33 716.67
## mod4.3 5 722.31 738.90 -356.16 712.31 4.3566 1 0.03687 *
## mod4.2 6 724.27 744.18 -356.14 712.27 0.0387 1 0.84408
## mod4.1 7 725.78 749.01 -355.89 711.78 0.4900 1 0.48395
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.5.week", "prec.5.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.5.week + nests.per.week$prec.5.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (5 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (5 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M5 <- gam(num.of.nests ~ s(temp.5.week) + s(prec.5.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M5) # some curvature
# build a maximal poisson model
mod5.1 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ I(z.prec.5.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod5.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## I(z.prec.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 722.8 746.0 -354.4 708.8 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1076 -0.7120 -0.3079 0.4180 4.7367
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.005716 0.07560
## year (Intercept) 0.008891 0.09429
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.02176 0.07806 13.090 <2e-16 ***
## z.temp.5.week -0.01264 0.06151 -0.206 0.8372
## z.prec.5.week 0.06530 0.08498 0.768 0.4422
## I(z.temp.5.week^2) -0.09307 0.04888 -1.904 0.0569 .
## I(z.prec.5.week^2) -0.08102 0.05203 -1.557 0.1194
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5. I(z.t.5.^2)
## z.temp.5.wk 0.329
## z.prec.5.wk 0.308 0.498
## I(z.t.5.^2) -0.367 -0.177 0.245
## I(z.p.5.^2) -0.459 -0.330 -0.736 -0.276
# remove quadratic precipitation term
mod5.2 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 723.3 743.2 -355.6 711.3 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0961 -0.7090 -0.2633 0.3951 4.6455
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.00761 0.08723
## year (Intercept) 0.01250 0.11179
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.96170 0.07109 13.527 <2e-16 ***
## z.temp.5.week -0.04392 0.05854 -0.750 0.4531
## z.prec.5.week -0.03561 0.05618 -0.634 0.5262
## I(z.temp.5.week^2) -0.11531 0.04710 -2.448 0.0143 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5.
## z.temp.5.wk 0.208
## z.prec.5.wk -0.032 0.416
## I(z.t.5.^2) -0.566 -0.293 0.046
# remove precipitation
mod5.3 <- glmer(num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 721.7 738.3 -355.8 711.7 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0985 -0.7372 -0.2578 0.3627 4.6901
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.006868 0.08287
## year (Intercept) 0.014592 0.12080
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.95931 0.07194 13.335 <2e-16 ***
## z.temp.5.week -0.02833 0.05321 -0.532 0.5945
## I(z.temp.5.week^2) -0.11410 0.04704 -2.426 0.0153 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5.
## z.temp.5.wk 0.241
## I(z.t.5.^2) -0.558 -0.344
# remove quadratic term
mod5.4 <- glmer(num.of.nests ~ z.temp.5.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod5.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.5.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 725.7 739.0 -358.9 717.7 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0396 -0.7816 -0.2384 0.4589 4.4220
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.023504 0.15331
## year (Intercept) 0.008584 0.09265
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.85009 0.05714 14.877 <2e-16 ***
## z.temp.5.week -0.07467 0.04789 -1.559 0.119
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.5.wk 0.072
# compare models using AIC
anova(mod5.1, mod5.2, mod5.3, mod5.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod5.4: num.of.nests ~ z.temp.5.week + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod5.2: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.2: (1 | year/week)
## mod5.1: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.1: I(z.prec.5.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod5.4 4 725.74 739.01 -358.87 717.74
## mod5.3 5 721.70 738.29 -355.85 711.70 6.0399 1 0.01399 *
## mod5.2 6 723.30 743.21 -355.65 711.30 0.3998 1 0.52721
## mod5.1 7 722.80 746.03 -354.40 708.80 2.4960 1 0.11413
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.6.week", "prec.6.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.6.week + nests.per.week$prec.6.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (6 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (6 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M6 <- gam(num.of.nests ~ s(temp.6.week) + s(prec.6.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M6) # only temperature seems to have curvature
# build a maximal poisson model
mod6.1 <- glmer(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ I(z.prec.6.week^2) + (1|year/week), family = "poisson" ,
data= nests.per.week)
summary(mod6.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## I(z.prec.6.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 722.1 745.3 -354.0 708.1 197
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1305 -0.7221 -0.2739 0.4146 4.7709
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.004297 0.06555
## year (Intercept) 0.009261 0.09623
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.028528 0.077861 13.210 <2e-16 ***
## z.temp.6.week -0.004595 0.061890 -0.074 0.9408
## z.prec.6.week 0.057117 0.082264 0.694 0.4875
## I(z.temp.6.week^2) -0.105622 0.048914 -2.159 0.0308 *
## I(z.prec.6.week^2) -0.075756 0.053884 -1.406 0.1598
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6. z.p.6. I(z.t.6.^2)
## z.temp.6.wk 0.302
## z.prec.6.wk 0.308 0.504
## I(z.t.6.^2) -0.323 -0.164 0.246
## I(z.p.6.^2) -0.474 -0.293 -0.703 -0.313
# remove quadratic term for precipitation
mod6.2 <- glmer(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ (1|year/week), family = "poisson", data= nests.per.week)
summary(mod6.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 722.1 742.0 -355.1 710.1 198
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1254 -0.6900 -0.2655 0.3627 4.6467
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.004537 0.06736
## year (Intercept) 0.014022 0.11842
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.97316 0.07078 13.750 < 2e-16 ***
## z.temp.6.week -0.02992 0.05982 -0.500 0.61695
## z.prec.6.week -0.02674 0.05783 -0.462 0.64387
## I(z.temp.6.week^2) -0.12811 0.04650 -2.755 0.00587 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6. z.p.6.
## z.temp.6.wk 0.189
## z.prec.6.wk -0.024 0.459
## I(z.t.6.^2) -0.547 -0.281 0.018
# remove precipitation term
mod6.3 <- glmer(num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2)
+ (1|year/week), family = "poisson", data= nests.per.week)
summary(mod6.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 720.3 736.9 -355.2 710.3 199
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.1276 -0.7087 -0.2483 0.3641 4.6985
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.003676 0.06063
## year (Intercept) 0.015836 0.12584
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.97188 0.07152 13.589 < 2e-16 ***
## z.temp.6.week -0.01716 0.05313 -0.323 0.74672
## I(z.temp.6.week^2) -0.12782 0.04646 -2.751 0.00594 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6.
## z.temp.6.wk 0.222
## I(z.t.6.^2) -0.540 -0.326
# remove quadratic term for temperature
mod6.4 <- glmer(num.of.nests ~ z.temp.6.week + (1|year/week),
family = "poisson", data= nests.per.week)
summary(mod6.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.6.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 726.1 739.4 -359.1 718.1 200
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.0250 -0.7791 -0.2251 0.4458 4.4396
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.024659 0.15703
## year (Intercept) 0.008442 0.09188
## Number of obs: 204, groups: week:year, 204; year, 16
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.85011 0.05711 14.884 <2e-16 ***
## z.temp.6.week -0.06807 0.04789 -1.422 0.155
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.6.wk 0.069
# compare models using anova
anova(mod6.1, mod6.2, mod6.3, mod6.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod6.4: num.of.nests ~ z.temp.6.week + (1 | year/week)
## mod6.3: num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## mod6.2: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.2: (1 | year/week)
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.1: I(z.prec.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod6.4 4 726.15 739.42 -359.07 718.15
## mod6.3 5 720.31 736.90 -355.16 710.31 7.8345 1 0.005126 **
## mod6.2 6 722.10 742.01 -355.05 710.10 0.2120 1 0.645230
## mod6.1 7 722.09 745.31 -354.04 708.09 2.0130 1 0.155954
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Compare all the models to choose the best model. Also compare final model with a model without random effects to check for their importance. Check for overdispersion in model.
# compare best models from different analyses
anova(mod1.3, mod2.3, mod3.3, mod4.3, mod5.3, mod6.3, test = "Chi")
## Data: nests.per.week
## Models:
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod2.3: num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2) + (1 | year/week)
## mod3.3: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod6.3: num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.3 5 724.88 741.47 -357.44 714.88
## mod2.3 5 722.22 738.81 -356.11 712.22 2.6681 0 <2e-16 ***
## mod3.3 5 723.47 740.06 -356.73 713.47 0.0000 0 1
## mod4.3 5 722.31 738.90 -356.16 712.31 1.1552 0 <2e-16 ***
## mod5.3 5 721.70 738.29 -355.85 711.70 0.6147 0 <2e-16 ***
## mod6.3 5 720.31 736.90 -355.16 710.31 1.3845 0 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# comparing best model with and without random effects
# run a model without random effects
M <- glm(num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2),
family = "poisson" , data= nests.per.week)
# compare the two models
anova(mod2.3, M, test = "Chi")
## Data: nests.per.week
## Models:
## M: num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2)
## mod2.3: num.of.nests ~ z.prec.2.week + I(z.prec.2.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## M 3 718.70 728.65 -356.35 712.70
## mod2.3 5 722.22 738.81 -356.11 712.22 0.4822 2 0.7858
# check if the model is overdispersed
dispersion_glmer(mod2.3) # not overdispersed
## [1] 0.9103993
Plot model results for the best model
# plot for effect of precipitation
plot_model(mod2.3, type = "pred", terms = "z.prec.2.week",
axis.title = c("Pre-laying precipitation average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "blue")
####### Temperature #######
# plotting pre-laying temperature by years on the same plot
nests.per.week$year <- as.factor(nests.per.week$year)
p <- ggplot(nests.per.week, aes(x=week, y=temp.2.week, col = year, group=year))
p + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average temperature for laying period by year and save as data frame
tmean <- as.data.frame(with(nests.per.week, tapply(temp.2.week, year, FUN = mean)))
# convert data frame's row names as the first column
tmean <- tibble::rownames_to_column(tmean, "year")
# check data frame
head(tmean)
## year with(nests.per.week, tapply(temp.2.week, year, FUN = mean))
## 1 1998 17.72546
## 2 1999 19.42738
## 3 2001 19.21978
## 4 2002 18.65134
## 5 2003 19.87817
## 6 2004 17.58690
# change name of second column
colnames(tmean)[2] <- "tmean"
# check data frame again
head(tmean)
## year tmean
## 1 1998 17.72546
## 2 1999 19.42738
## 3 2001 19.21978
## 4 2002 18.65134
## 5 2003 19.87817
## 6 2004 17.58690
####### Precipitation ########
# plot pre-laying period precipitation
P <- ggplot(nests.per.week, aes(x=week, y=prec.2.week, col = year, group=year))
P + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average precipitation for laying period by year and save as data frame
pmean <- as.data.frame(with(nests.per.week,
tapply(prec.2.week, year, FUN = mean, na.rm = T)))
# convert data frame's row names as the first column
pmean <- tibble::rownames_to_column(pmean, "year")
# check data frame
head(pmean)
## year
## 1 1998
## 2 1999
## 3 2001
## 4 2002
## 5 2003
## 6 2004
## with(nests.per.week, tapply(prec.2.week, year, FUN = mean, na.rm = T))
## 1 0.9223443
## 2 0.5081633
## 3 0.1919414
## 4 1.1616071
## 5 1.4579365
## 6 1.0206349
# change name of second column
colnames(pmean)[2] <- "pmean"
# check data frame again
head(pmean)
## year pmean
## 1 1998 0.9223443
## 2 1999 0.5081633
## 3 2001 0.1919414
## 4 2002 1.1616071
## 5 2003 1.4579365
## 6 2004 1.0206349
# plots for climatic trends at Galicia
# set page layout to incorporate two plots side by side
par(mfrow = c(1,2))
# set the axis lables to be slightly bigger
par(cex.lab=1.4)
# plot annual mean temperature for the laying period vs year
plot(tmean$tmean ~ tmean$year,
main="Annual breeding season mean temperature (Galicia)",
ylab="Temperature (Degree celsius)", xlab="Year",
pch = 18, cex = 2, col = "red", xaxt="n", cex.main = 1)
# costumize x axis ticks, make them vertical
axis(1, at = seq(1998, 2017, by = 1), las=2)
# plot annual mean precipitation for the laying period vs year
plot(pmean$pmean ~ pmean$year,
main="Annual breeding season mean precipitation (Galicia)",
ylab="Precipitation (mm)", xlab="Year",
pch = 18, cex = 2, col = "blue", xaxt="n", cex.main = 1)
# costumize x axis ticks, make them vertical
axis(1, at = seq(1998, 2017, by = 1), las=2)
###### correlation test for climate data #######
# convert year to numeric
tmean$year <- as.numeric(tmean$year)
# test for correlation using spearman's correlation test
cor.test(tmean$tmean, tmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: tmean$tmean and tmean$year
## S = 754, p-value = 0.6886
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.1088235
# repeat for precipitation data
# convert year to numeric
pmean$year <- as.numeric(pmean$year)
# test for correlation using spearman's correlation test
cor.test(pmean$pmean, pmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: pmean$pmean and pmean$year
## S = 376, p-value = 0.08437
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4470588
Repeat all analysis for data from the Samouco population.
Some descriptive statistics for the dataset.
# load final dataset for Hungary
nests.per.week <- read.csv("../Data/Samouco_numofnests.csv")
# check loaded dataset
head(nests.per.week)
## X week year num.of.nests X.x pmean prec.lag1 prec.1.week
## 1 1 11 2005 3 12 0.02857143 0.40000000 1.7285714
## 2 2 12 2005 4 13 1.22857143 0.02857143 0.2142857
## 3 3 12 2011 1 331 0.00000000 11.92857143 8.6714286
## 4 4 13 2006 1 66 0.00000000 5.00000000 4.0357143
## 5 5 13 2007 1 120 0.25714286 2.75714286 1.3785714
## 6 6 13 2011 3 332 0.14285714 0.00000000 5.9642857
## prec.2.week prec.3.week prec.4.week prec.5.week prec.6.week X.y tmean
## 1 1.2380952 0.9285714 1.2828571 1.104762 0.9469388 12 15.90000
## 2 1.1619048 0.9357143 0.7485714 1.073810 0.9510204 13 16.97143
## 3 7.7904762 5.8428571 6.1514286 5.126190 4.3938776 331 16.14286
## 4 2.6904762 2.8785714 2.8028571 2.521429 2.1612245 66 16.80000
## 5 0.9190476 0.6892857 0.5685714 1.133333 1.0857143 120 11.80000
## 6 5.7809524 5.8428571 4.6742857 5.126190 4.3938776 332 17.38571
## temp.lag1 temp.1.week temp.2.week temp.3.week temp.4.week temp.5.week
## 1 11.52857 9.792857 9.742857 9.782143 9.862857 9.554762
## 2 15.90000 13.714286 11.828571 11.282143 11.005714 10.869048
## 3 13.77143 13.471429 12.847619 13.510714 13.131429 12.923810
## 4 14.80000 14.610714 14.040476 13.305357 12.472857 12.465476
## 5 13.15714 12.842857 13.352381 13.778571 14.014286 13.750000
## 6 16.14286 14.957143 14.361905 13.671429 14.037143 13.633333
## temp.6.week
## 1 9.195918
## 2 10.461224
## 3 12.453061
## 4 12.339796
## 5 13.967347
## 6 13.383673
# check dimensions of the dataset
dim(nests.per.week)
## [1] 123 22
# check median number of nests per week
median(nests.per.week$num.of.nests)
## [1] 4
# check standard deviation of number of nests per week
sd(nests.per.week$num.of.nests)
## [1] 3.870092
# check range of number of nests per week
range(nests.per.week$num.of.nests)
## [1] 1 17
Some preliminary anaylysis before modelling to check for model assumptions and standardization of predictor variables.
# check for outliers
boxplot(nests.per.week$num.of.nests ~ nests.per.week$year, xlab = "Year",
ylab = "Number of nests per week", main = "Number of nests per week
for each year (Samouco)")
# check for homogeneitry of variances
with(nests.per.week, tapply(num.of.nests, year, FUN = var))
## 2005 2006 2007 2010 2011 2012 2013
## 4.735294 4.335165 7.272727 12.692308 21.035948 16.383333 11.563636
## 2014 2017
## 15.670330 22.694444
table(nests.per.week$year)
##
## 2005 2006 2007 2010 2011 2012 2013 2014 2017
## 17 14 11 13 18 16 11 14 9
# check for normality and zero inflation
hist(nests.per.week$num.of.nests, breaks = 30) # no zero inflation
# standardize predictor variables
nests.per.week$z.temp.1.week <- scale(nests.per.week$temp.1.week)
nests.per.week$z.prec.1.week <- scale(nests.per.week$prec.1.week)
nests.per.week$z.temp.2.week <- scale(nests.per.week$temp.2.week)
nests.per.week$z.prec.2.week <- scale(nests.per.week$prec.2.week)
nests.per.week$z.temp.3.week <- scale(nests.per.week$temp.3.week)
nests.per.week$z.prec.3.week <- scale(nests.per.week$prec.3.week)
nests.per.week$z.temp.4.week <- scale(nests.per.week$temp.4.week)
nests.per.week$z.prec.4.week <- scale(nests.per.week$prec.4.week)
nests.per.week$z.temp.5.week <- scale(nests.per.week$temp.5.week)
nests.per.week$z.prec.5.week <- scale(nests.per.week$prec.5.week)
nests.per.week$z.temp.6.week <- scale(nests.per.week$temp.6.week)
nests.per.week$z.prec.6.week <- scale(nests.per.week$prec.6.week)
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.1.week", "prec.1.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.1.week + nests.per.week$prec.1.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (1 week) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot number of nests per week vs pre-laying period temperature
qplot(temp.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (1 week) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M1 <- gam(num.of.nests ~ s(temp.1.week) + s(prec.1.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M1) # seems to have curvature
# build a maximal poisson model
mod1.1 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ I(z.prec.1.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## I(z.prec.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 633.6 653.3 -309.8 619.6 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2177 -0.5966 -0.1073 0.3620 1.4574
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1732 0.4162
## year (Intercept) 0.1369 0.3701
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.81535 0.15092 12.029 < 2e-16 ***
## z.temp.1.week 0.02280 0.07503 0.304 0.761244
## z.prec.1.week 0.22718 0.12257 1.853 0.063817 .
## I(z.temp.1.week^2) -0.22869 0.06351 -3.601 0.000317 ***
## I(z.prec.1.week^2) -0.04663 0.04023 -1.159 0.246419
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1. I(z.t.1.^2)
## z.temp.1.wk -0.007
## z.prec.1.wk 0.188 0.380
## I(z.t.1.^2) -0.344 0.126 0.055
## I(z.p.1.^2) -0.210 -0.167 -0.833 -0.099
# remove quadratic term for precipitation
mod1.2 <- glmer(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod1.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 632.9 649.8 -310.5 620.9 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2242 -0.5947 -0.1077 0.4480 1.6094
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.177 0.4207
## year (Intercept) 0.128 0.3577
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.776804 0.144511 12.295 < 2e-16 ***
## z.temp.1.week 0.008028 0.074371 0.108 0.914044
## z.prec.1.week 0.106868 0.067438 1.585 0.113038
## I(z.temp.1.week^2) -0.236559 0.063782 -3.709 0.000208 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1. z.p.1.
## z.temp.1.wk -0.044
## z.prec.1.wk 0.030 0.443
## I(z.t.1.^2) -0.387 0.108 -0.062
# remove precipitation term
mod1.3 <- glmer(num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod1.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 633.3 647.4 -311.6 623.3 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2431 -0.6027 -0.1404 0.4440 1.4208
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1897 0.4355
## year (Intercept) 0.1248 0.3532
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.76639 0.14374 12.289 < 2e-16 ***
## z.temp.1.week -0.04392 0.06795 -0.646 0.518042
## I(z.temp.1.week^2) -0.23099 0.06407 -3.605 0.000312 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.1.
## z.temp.1.wk -0.068
## I(z.t.1.^2) -0.390 0.159
# remove quadratic term for temperature
mod1.4 <- glmer(num.of.nests ~ z.temp.1.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod1.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.1.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.6 -318.2 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.26935 -0.61130 -0.04538 0.44740 1.08959
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.23994 0.4898
## year (Intercept) 0.07786 0.2790
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.541323 0.112893 13.653 <2e-16 ***
## z.temp.1.week 0.001885 0.067871 0.028 0.978
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.1.wk -0.010
# compare models using AIC
anova(mod1.1, mod1.2, mod1.3, mod1.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod1.4: num.of.nests ~ z.temp.1.week + (1 | year/week)
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod1.2: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.2: (1 | year/week)
## mod1.1: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## mod1.1: I(z.prec.1.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.4 4 644.31 655.55 -318.15 636.31
## mod1.3 5 633.30 647.36 -311.65 623.30 13.0084 1 0.0003101 ***
## mod1.2 6 632.91 649.78 -310.46 620.91 2.3863 1 0.1224054
## mod1.1 7 633.57 653.26 -309.79 619.57 1.3367 1 0.2476108
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.2.week", "prec.2.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.2.week + nests.per.week$prec.2.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (2 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (2 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M2 <- gam(num.of.nests ~ s(temp.2.week) + s(prec.2.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M2) # only temperature has curvature
# build a maximal poisson model
mod2.1 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ I(z.prec.2.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod2.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## I(z.prec.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 631.2 650.8 -308.6 617.2 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.4059 -0.6367 -0.1289 0.4662 1.5079
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1622 0.4028
## year (Intercept) 0.1342 0.3663
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.81599 0.14805 12.266 < 2e-16 ***
## z.temp.2.week 0.07194 0.07903 0.910 0.362646
## z.prec.2.week 0.28076 0.11814 2.376 0.017478 *
## I(z.temp.2.week^2) -0.21577 0.06257 -3.448 0.000564 ***
## I(z.prec.2.week^2) -0.05723 0.04040 -1.416 0.156644
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2. I(z.t.2.^2)
## z.temp.2.wk -0.003
## z.prec.2.wk 0.143 0.484
## I(z.t.2.^2) -0.328 0.140 0.134
## I(z.p.2.^2) -0.178 -0.216 -0.788 -0.173
# remove quadratic precipitation term
mod2.2 <- glmer(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 631.2 648.1 -309.6 619.2 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.3405 -0.6771 -0.1162 0.4701 1.6754
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1659 0.4073
## year (Intercept) 0.1241 0.3523
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.77618 0.14220 12.491 < 2e-16 ***
## z.temp.2.week 0.04712 0.07757 0.607 0.543530
## z.prec.2.week 0.14568 0.07205 2.022 0.043180 *
## I(z.temp.2.week^2) -0.23234 0.06216 -3.738 0.000186 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2. z.p.2.
## z.temp.2.wk -0.042
## z.prec.2.wk 0.014 0.522
## I(z.t.2.^2) -0.384 0.100 -0.027
# remove precipitation
mod2.3 <- glmer(num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod2.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 633.0 647.1 -311.5 623.0 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2530 -0.6442 -0.1417 0.4952 1.3538
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1848 0.4299
## year (Intercept) 0.1232 0.3510
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.76646 0.14259 12.388 < 2e-16 ***
## z.temp.2.week -0.03478 0.06812 -0.511 0.609576
## I(z.temp.2.week^2) -0.23011 0.06276 -3.667 0.000246 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.2.
## z.temp.2.wk -0.064
## I(z.t.2.^2) -0.387 0.147
# remove quadratic term
mod2.4 <- glmer(num.of.nests ~ z.temp.2.week + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod2.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.2.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.5 -318.1 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.26072 -0.60452 -0.04305 0.44497 1.08868
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.2402 0.4901
## year (Intercept) 0.0762 0.2760
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.541208 0.112081 13.75 <2e-16 ***
## z.temp.2.week 0.009533 0.068315 0.14 0.889
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.2.wk -0.011
# compare models using AIC
anova(mod2.1, mod2.2, mod2.3, mod2.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod2.4: num.of.nests ~ z.temp.2.week + (1 | year/week)
## mod2.3: num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | year/week)
## mod2.2: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.2: (1 | year/week)
## mod2.1: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.1: I(z.prec.2.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod2.4 4 644.29 655.54 -318.14 636.29
## mod2.3 5 633.04 647.10 -311.52 623.04 13.2484 1 0.0002728 ***
## mod2.2 6 631.18 648.05 -309.59 619.18 3.8578 1 0.0495144 *
## mod2.1 7 631.15 650.84 -308.58 617.15 2.0305 1 0.1541664
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.3.week", "prec.3.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.3.week + nests.per.week$prec.3.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (3 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (3 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M3 <- gam(num.of.nests ~ s(temp.3.week) + s(prec.3.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M3) # some curvature
# build a maximal poisson model
mod3.1 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ I(z.prec.3.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod3.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## I(z.prec.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 624.0 643.6 -305.0 610.0 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.4615 -0.6674 -0.1415 0.4743 2.0485
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1304 0.3611
## year (Intercept) 0.1507 0.3882
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.85346 0.15353 12.072 < 2e-16 ***
## z.temp.3.week 0.13306 0.07958 1.672 0.09452 .
## z.prec.3.week 0.33914 0.11630 2.916 0.00354 **
## I(z.temp.3.week^2) -0.24122 0.06147 -3.924 8.7e-05 ***
## I(z.prec.3.week^2) -0.06373 0.04714 -1.352 0.17640
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3. I(z.t.3.^2)
## z.temp.3.wk 0.032
## z.prec.3.wk 0.160 0.532
## I(z.t.3.^2) -0.290 0.076 0.147
## I(z.p.3.^2) -0.207 -0.229 -0.769 -0.200
# remove quadratic term for precipitation
mod3.2 <- glmer(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod3.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 623.8 640.6 -305.9 611.8 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.45818 -0.65551 -0.09596 0.49841 2.18368
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1333 0.3652
## year (Intercept) 0.1386 0.3723
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.80758 0.14603 12.378 < 2e-16 ***
## z.temp.3.week 0.10750 0.07767 1.384 0.1663
## z.prec.3.week 0.21600 0.07398 2.920 0.0035 **
## I(z.temp.3.week^2) -0.25807 0.06074 -4.249 2.15e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3. z.p.3.
## z.temp.3.wk -0.015
## z.prec.3.wk 0.008 0.570
## I(z.t.3.^2) -0.361 0.026 -0.028
# remove precipitaion
mod3.3 <- glmer(num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 629.7 643.7 -309.8 619.7 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2780 -0.6342 -0.1647 0.4892 1.5299
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1697 0.4119
## year (Intercept) 0.1288 0.3588
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.79306 0.14375 12.473 < 2e-16 ***
## z.temp.3.week -0.02234 0.06719 -0.332 0.74
## I(z.temp.3.week^2) -0.25576 0.06177 -4.140 3.47e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.3.
## z.temp.3.wk -0.040
## I(z.t.3.^2) -0.374 0.089
# remove quadratic temperature term
mod3.4 <- glmer(num.of.nests ~ z.temp.3.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod3.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.3.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.5 -318.1 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.25618 -0.60154 -0.04056 0.44275 1.08940
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.24026 0.4902
## year (Intercept) 0.07546 0.2747
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.54116 0.11171 13.795 <2e-16 ***
## z.temp.3.week 0.01288 0.06868 0.187 0.851
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.3.wk -0.012
# compare models using AIC
anova(mod3.1, mod3.2, mod3.3, mod3.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod3.4: num.of.nests ~ z.temp.3.week + (1 | year/week)
## mod3.3: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | year/week)
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.2: (1 | year/week)
## mod3.1: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.1: I(z.prec.3.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod3.4 4 644.27 655.52 -318.14 636.27
## mod3.3 5 629.65 643.71 -309.83 619.65 16.6178 1 4.572e-05 ***
## mod3.2 6 623.78 640.65 -305.89 611.78 7.8787 1 0.005002 **
## mod3.1 7 623.95 643.64 -304.98 609.95 1.8229 1 0.176964
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.4.week", "prec.4.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.4.week + nests.per.week$prec.4.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (4 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (4 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M4 <- gam(num.of.nests ~ s(temp.4.week) + s(prec.4.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M4) # seems to have curvature
# build a maximal poisson model
mod4.1 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ I(z.prec.4.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod4.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## I(z.prec.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 628.7 648.4 -307.3 614.7 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.3270 -0.6763 -0.1198 0.4856 1.5321
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1560 0.3950
## year (Intercept) 0.1277 0.3574
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.83032 0.14691 12.459 < 2e-16 ***
## z.temp.4.week 0.07844 0.08501 0.923 0.3561
## z.prec.4.week 0.20371 0.12024 1.694 0.0902 .
## I(z.temp.4.week^2) -0.24361 0.06201 -3.929 8.54e-05 ***
## I(z.prec.4.week^2) -0.04455 0.05115 -0.871 0.3837
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4. I(z.t.4.^2)
## z.temp.4.wk 0.026
## z.prec.4.wk 0.159 0.582
## I(z.t.4.^2) -0.296 0.152 0.233
## I(z.p.4.^2) -0.248 -0.270 -0.758 -0.208
# remove quadratic precipitation term
mod4.2 <- glmer(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 627.4 644.3 -307.7 615.4 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2909 -0.6784 -0.1341 0.5035 1.5728
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1571 0.3964
## year (Intercept) 0.1191 0.3452
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.79764 0.13902 12.930 < 2e-16 ***
## z.temp.4.week 0.05824 0.08193 0.711 0.477
## z.prec.4.week 0.12335 0.07787 1.584 0.113
## I(z.temp.4.week^2) -0.25526 0.06076 -4.201 2.66e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4. z.p.4.
## z.temp.4.wk -0.044
## z.prec.4.wk -0.044 0.601
## I(z.t.4.^2) -0.376 0.099 0.110
# remove precipitation
mod4.3 <- glmer(num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 627.8 641.9 -308.9 617.8 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2117 -0.6480 -0.1655 0.4490 1.4276
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1654 0.4067
## year (Intercept) 0.1307 0.3615
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.80410 0.14383 12.543 < 2e-16 ***
## z.temp.4.week -0.02093 0.06660 -0.314 0.753
## I(z.temp.4.week^2) -0.26677 0.06077 -4.390 1.14e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.4.
## z.temp.4.wk -0.027
## I(z.t.4.^2) -0.364 0.056
# remove quadratic term
mod4.4 <- glmer(num.of.nests ~ z.temp.4.week
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod4.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.4.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.5 -318.1 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.26407 -0.60752 -0.04339 0.44465 1.09018
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.24011 0.4900
## year (Intercept) 0.07694 0.2774
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.541245 0.112447 13.706 <2e-16 ***
## z.temp.4.week 0.006075 0.068661 0.088 0.929
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.4.wk -0.012
# compare models using AIC
anova(mod4.1, mod4.2, mod4.3, mod4.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod4.4: num.of.nests ~ z.temp.4.week + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod4.2: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.2: (1 | year/week)
## mod4.1: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.1: I(z.prec.4.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod4.4 4 644.30 655.55 -318.15 636.30
## mod4.3 5 627.83 641.89 -308.92 617.83 18.4662 1 1.729e-05 ***
## mod4.2 6 627.43 644.30 -307.71 615.43 2.4054 1 0.1209
## mod4.1 7 628.67 648.35 -307.33 614.67 0.7585 1 0.3838
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.5.week", "prec.5.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.5.week + nests.per.week$prec.5.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (5 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (5 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M5 <- gam(num.of.nests ~ s(temp.5.week) + s(prec.5.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M5) # only temperature has curvature
# build a maximal poisson model
mod5.1 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ I(z.prec.5.week^2) + (1|year/week), family = "poisson",
data= nests.per.week)
summary(mod5.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## I(z.prec.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 625.7 645.4 -305.8 611.7 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2977 -0.6527 -0.1606 0.4588 1.6741
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1484 0.3853
## year (Intercept) 0.1331 0.3648
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.85924 0.14916 12.465 < 2e-16 ***
## z.temp.5.week 0.04065 0.08806 0.462 0.644
## z.prec.5.week 0.13058 0.11708 1.115 0.265
## I(z.temp.5.week^2) -0.27304 0.06328 -4.315 1.6e-05 ***
## I(z.prec.5.week^2) -0.04881 0.05834 -0.837 0.403
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5. I(z.t.5.^2)
## z.temp.5.wk 0.016
## z.prec.5.wk 0.133 0.621
## I(z.t.5.^2) -0.257 0.201 0.334
## I(z.p.5.^2) -0.265 -0.265 -0.711 -0.274
# remove quadratic precipitation term
mod5.2 <- glmer(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 624.4 641.3 -306.2 612.4 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2732 -0.6668 -0.1589 0.4644 1.7947
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1498 0.3871
## year (Intercept) 0.1250 0.3536
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.82536 0.14079 12.965 < 2e-16 ***
## z.temp.5.week 0.02120 0.08517 0.249 0.803
## z.prec.5.week 0.06049 0.08186 0.739 0.460
## I(z.temp.5.week^2) -0.28797 0.06103 -4.719 2.37e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5. z.p.5.
## z.temp.5.wk -0.060
## z.prec.5.wk -0.082 0.638
## I(z.t.5.^2) -0.365 0.136 0.198
# remove precipitation
mod5.3 <- glmer(num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2)
+ (1|year/week), family = "poisson" , data= nests.per.week)
summary(mod5.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 622.9 637.0 -306.5 612.9 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2476 -0.6887 -0.1490 0.4682 1.7411
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1494 0.3865
## year (Intercept) 0.1364 0.3693
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.83322 0.14469 12.670 < 2e-16 ***
## z.temp.5.week -0.01942 0.06571 -0.296 0.768
## I(z.temp.5.week^2) -0.29717 0.05975 -4.973 6.59e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.5.
## z.temp.5.wk -0.011
## I(z.t.5.^2) -0.346 0.017
# remove quadratic term
mod5.4 <- glmer(num.of.nests ~ z.temp.5.week + (1|year/week),
family = "poisson" , data= nests.per.week)
summary(mod5.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.5.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.6 -318.2 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.27099 -0.61255 -0.04668 0.44754 1.09035
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.23989 0.4898
## year (Intercept) 0.07817 0.2796
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.5413384 0.1130449 13.635 <2e-16 ***
## z.temp.5.week 0.0004393 0.0687939 0.006 0.995
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.5.wk -0.011
# compare models using AIC
anova(mod5.1, mod5.2, mod5.3, mod5.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod5.4: num.of.nests ~ z.temp.5.week + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod5.2: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.2: (1 | year/week)
## mod5.1: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## mod5.1: I(z.prec.5.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod5.4 4 644.31 655.55 -318.15 636.31
## mod5.3 5 622.92 636.98 -306.46 612.92 23.3886 1 1.324e-06 ***
## mod5.2 6 624.39 641.26 -306.19 612.39 0.5316 1 0.4659
## mod5.1 7 625.69 645.38 -305.85 611.69 0.6942 1 0.4047
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.6.week", "prec.6.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.6.week + nests.per.week$prec.6.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (6 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (6 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~year, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M6 <- gam(num.of.nests ~ s(temp.6.week) + s(prec.6.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M6) # only temperature seems to have curvature
# build a maximal poisson model
mod6.1 <- glmer(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ I(z.prec.6.week^2) + (1|year/week), family = "poisson" ,
data= nests.per.week)
summary(mod6.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## I(z.prec.6.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 623.1 642.8 -304.6 609.1 116
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.3065 -0.6516 -0.2221 0.4968 1.7118
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1394 0.3734
## year (Intercept) 0.1416 0.3764
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.88208 0.15337 12.272 < 2e-16 ***
## z.temp.6.week 0.01004 0.08836 0.114 0.910
## z.prec.6.week 0.10758 0.11060 0.973 0.331
## I(z.temp.6.week^2) -0.28189 0.06369 -4.426 9.6e-06 ***
## I(z.prec.6.week^2) -0.06470 0.06454 -1.002 0.316
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6. z.p.6. I(z.t.6.^2)
## z.temp.6.wk -0.031
## z.prec.6.wk 0.082 0.628
## I(z.t.6.^2) -0.240 0.249 0.398
## I(z.p.6.^2) -0.289 -0.178 -0.607 -0.281
# rremove quadratic term for precipitation
mod6.2 <- glmer(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ (1|year/week), family = "poisson", data= nests.per.week)
summary(mod6.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 622.1 639.0 -305.1 610.1 117
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2946 -0.6738 -0.1720 0.4788 1.8718
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1414 0.3761
## year (Intercept) 0.1310 0.3619
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.83639 0.14289 12.852 <2e-16 ***
## z.temp.6.week -0.00550 0.08700 -0.063 0.950
## z.prec.6.week 0.04022 0.08712 0.462 0.644
## I(z.temp.6.week^2) -0.30018 0.06137 -4.891 1e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6. z.p.6.
## z.temp.6.wk -0.088
## z.prec.6.wk -0.123 0.662
## I(z.t.6.^2) -0.361 0.205 0.289
# remove precipitation term
mod6.3 <- glmer(num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2)
+ (1|year/week), family = "poisson", data= nests.per.week)
summary(mod6.3)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 620.3 634.4 -305.2 610.3 118
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.2860 -0.6971 -0.1783 0.4837 1.8529
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.1396 0.3736
## year (Intercept) 0.1409 0.3754
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.84430 0.14554 12.672 < 2e-16 ***
## z.temp.6.week -0.03233 0.06512 -0.497 0.62
## I(z.temp.6.week^2) -0.30849 0.05861 -5.264 1.41e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) z.t.6.
## z.temp.6.wk -0.010
## I(z.t.6.^2) -0.332 0.021
# remove quadratic term for temperature
mod6.4 <- glmer(num.of.nests ~ z.temp.6.week + (1|year/week),
family = "poisson", data= nests.per.week)
summary(mod6.4)
## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.6.week + (1 | year/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 655.5 -318.1 636.3 119
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.28366 -0.60019 -0.05869 0.44876 1.09296
##
## Random effects:
## Groups Name Variance Std.Dev.
## week:year (Intercept) 0.23943 0.4893
## year (Intercept) 0.08028 0.2833
## Number of obs: 123, groups: week:year, 123; year, 9
##
## Fixed effects:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.541511 0.114062 13.515 <2e-16 ***
## z.temp.6.week -0.009148 0.068770 -0.133 0.894
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## z.temp.6.wk -0.010
# compare models using anova
anova(mod6.1, mod6.2, mod6.3, mod6.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod6.4: num.of.nests ~ z.temp.6.week + (1 | year/week)
## mod6.3: num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## mod6.2: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.2: (1 | year/week)
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## mod6.1: I(z.prec.6.week^2) + (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod6.4 4 644.29 655.54 -318.14 636.29
## mod6.3 5 620.33 634.39 -305.16 610.33 25.9609 1 3.484e-07 ***
## mod6.2 6 622.12 638.99 -305.06 610.12 0.2080 1 0.6484
## mod6.1 7 623.13 642.81 -304.56 609.13 0.9937 1 0.3188
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Compare all the models to choose the best model. Also compare final model with a model without random effects to check for their importance. Check for model overdispersion.
# compare best models from different analyses
anova(mod1.3, mod2.2, mod3.2, mod4.2, mod4.3, mod5.3, mod6.3, test= "Chi")
## Data: nests.per.week
## Models:
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | year/week)
## mod4.3: num.of.nests ~ z.temp.4.week + I(z.temp.4.week^2) + (1 | year/week)
## mod5.3: num.of.nests ~ z.temp.5.week + I(z.temp.5.week^2) + (1 | year/week)
## mod6.3: num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | year/week)
## mod2.2: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## mod2.2: (1 | year/week)
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.2: (1 | year/week)
## mod4.2: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## mod4.2: (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.3 5 633.30 647.36 -311.65 623.30
## mod4.3 5 627.83 641.89 -308.92 617.83 5.4647 0 <2e-16 ***
## mod5.3 5 622.92 636.98 -306.46 612.92 4.9148 0 <2e-16 ***
## mod6.3 5 620.33 634.39 -305.16 610.33 2.5893 0 <2e-16 ***
## mod2.2 6 631.18 648.05 -309.59 619.18 0.0000 1 1
## mod3.2 6 623.78 640.65 -305.89 611.78 7.4057 0 <2e-16 ***
## mod4.2 6 627.43 644.30 -307.71 615.43 0.0000 0 1
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# comparing best model with and without random effects
# run a model without random effects
M <- glm(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ I(z.prec.3.week^2), family = "poisson" , data= nests.per.week)
# compare the two models
anova(mod3.2, M, test = "Chi")
## Data: nests.per.week
## Models:
## M: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## M: I(z.prec.3.week^2)
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## mod3.2: (1 | year/week)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## M 5 695.54 709.60 -342.77 685.54
## mod3.2 6 623.78 640.65 -305.89 611.78 73.761 1 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# check if the model is overdispersed
dispersion_glmer(mod3.2) # not overdispersed
## [1] 1.00879
Plot model results for the best model
# install package strengejacke for sjPlot to work properly
devtools::install_github("strengejacke/strengejacke", force = TRUE)
## Downloading GitHub repo strengejacke/strengejacke@master
## from URL https://api.github.com/repos/strengejacke/strengejacke/zipball/master
## Installing strengejacke
## '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file \
## --no-environ --no-save --no-restore --quiet CMD INSTALL \
## '/private/var/folders/_g/g0tfx7tn3qv6f6qtzkppkxd00000gn/T/RtmpVKY5fh/devtools2bc07487382b/strengejacke-strengejacke-d41d521' \
## --library='/Library/Frameworks/R.framework/Versions/3.5/Resources/library' \
## --install-tests
##
# plot for effect of precipitation
p1 <- plot_model(mod3.2, type = "pred", terms = "z.prec.3.week",
axis.title = c("Pre-laying precipitation average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "blue")
# plot for effect of temperature
p2 <- plot_model(mod3.2, type = "pred", terms = "z.temp.3.week",
axis.title = c("Pre-laying temperature average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "red")
# arrange the plots together
grid.arrange(p1, p2, nrow = 1, ncol = 2)
####### Temperature #######
# plotting pre-laying temperature by years on the same plot
nests.per.week$year <- as.factor(nests.per.week$year)
p <- ggplot(nests.per.week, aes(x=week, y=temp.3.week, col = year, group=year))
p + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average temperature for laying period by year and save as data frame
tmean <- as.data.frame(with(nests.per.week, tapply(temp.3.week, year, FUN = mean)))
# convert data frame's row names as the first column
tmean <- tibble::rownames_to_column(tmean, "year")
# check data frame
head(tmean)
## year with(nests.per.week, tapply(temp.3.week, year, FUN = mean))
## 1 2005 19.53508
## 2 2006 18.61263
## 3 2007 17.84481
## 4 2010 19.29560
## 5 2011 20.00754
## 6 2012 19.07121
# change name of second column
colnames(tmean)[2] <- "tmean"
# check data frame again
head(tmean)
## year tmean
## 1 2005 19.53508
## 2 2006 18.61263
## 3 2007 17.84481
## 4 2010 19.29560
## 5 2011 20.00754
## 6 2012 19.07121
####### Precipitation ########
# plot pre-laying period precipitation
P <- ggplot(nests.per.week, aes(x=week, y=prec.3.week, col = year, group=year))
P + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average precipitation for laying period by year and save as data frame
pmean <- as.data.frame(with(nests.per.week,
tapply(prec.3.week, year, FUN = mean, na.rm = T)))
# convert data frame's row names as the first column
pmean <- tibble::rownames_to_column(pmean, "year")
# check data frame
head(pmean)
## year
## 1 2005
## 2 2006
## 3 2007
## 4 2010
## 5 2011
## 6 2012
## with(nests.per.week, tapply(prec.3.week, year, FUN = mean, na.rm = T))
## 1 0.5241597
## 2 1.3071429
## 3 0.8246753
## 4 1.1870879
## 5 2.3402778
## 6 0.6457589
# change name of second column
colnames(pmean)[2] <- "pmean"
# check data frame again
head(pmean)
## year pmean
## 1 2005 0.5241597
## 2 2006 1.3071429
## 3 2007 0.8246753
## 4 2010 1.1870879
## 5 2011 2.3402778
## 6 2012 0.6457589
# plots for climatic trends at Samouco
# set page layout to incorporate two plots side by side
par(mfrow = c(1,2))
# set the axis lables to be slightly bigger
par(cex.lab=1.4)
# plot annual mean temperature for the laying period vs year
plot(tmean$tmean ~ tmean$year,
main="Annual breeding season mean temperature (Samouco)",
ylab="Temperature (Degree celsius)", xlab="Year",
pch = 18, cex = 2, col = "red", xaxt = "n", cex.main = 1)
# customize x axis ticks, set them to be vertical
axis(1, at = seq(2006, 2017, by = 1), las=2)
# plot annual mean precipitation for the laying period vs year
plot(pmean$pmean ~ pmean$year,
main="Annual breeding season mean precipitation (Samouco)",
ylab="Precipitation (mm)", xlab="Year",
pch = 18, cex = 2, col = "blue", xaxt = "n", cex.main = 1)
axis(1, at = seq(2006, 2017, by = 1), las=2)
# customize x axis ticks, set them to be vertical
pmean$year <- as.numeric(pmean$year)
###### correlation test for climate data #######
# convert year to numeric
tmean$year <- as.numeric(tmean$year)
# test for correlation using spearman's correlation test
cor.test(tmean$tmean, tmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: tmean$tmean and tmean$year
## S = 72, p-value = 0.2912
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4
# repeat for precipitation data
# convert year to numeric
pmean$year <- as.numeric(pmean$year)
# test for correlation using spearman's correlation test
cor.test(pmean$pmean, pmean$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: pmean$pmean and pmean$year
## S = 154, p-value = 0.463
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.2833333
Repeat all analysis for population at Cheetham.
Some descriptive statistics for the dataset.
# load final dataset for Hungary
nests.per.week <- read.csv("../Data/Cheetham_numofnests.csv")
# check loaded dataset
head(nests.per.week)
## X week breeding_season num.of.nests X.x pmean prec.lag1 prec.1.week
## 1 1 10 2010/11 1 11 3.5428571 2.5142857 1.7857143
## 2 2 10 2011/12 1 64 0.4571429 0.0000000 0.5142857
## 3 3 10 2012/13 2 116 1.2285714 0.6285714 0.5571429
## 4 4 10 2013/14 1 169 1.4000000 4.0000000 2.2428571
## 5 5 10 2014/15 1 222 1.4571429 2.2000000 1.1142857
## 6 6 11 2010/11 1 12 0.9714286 3.5428571 3.0285714
## prec.2.week prec.3.week prec.4.week prec.5.week prec.6.week X.y
## 1 1.4476190 1.2642857 1.0171429 1.2000000 1.1673469 11
## 2 0.6476190 0.6642857 0.6571429 0.5523810 0.8040816 64
## 3 0.4571429 0.7357143 0.8800000 0.8523810 1.4204082 116
## 4 2.3142857 2.4500000 2.1542857 1.8000000 1.5510204 169
## 5 0.9238095 0.9428571 0.9200000 0.9714286 0.9959184 222
## 6 2.3714286 1.9714286 1.7200000 1.4380952 1.5346939 12
## tmean temp.lag1 temp.1.week temp.2.week temp.3.week temp.4.week
## 1 8.928571 10.400000 9.907143 9.457143 9.860714 9.557143
## 2 9.114286 14.414286 11.628571 10.980952 10.532143 10.477143
## 3 9.814286 9.000000 9.721429 10.014286 10.078571 9.665714
## 4 11.014286 9.957143 10.492857 10.052381 10.939286 10.325714
## 5 9.128571 8.314286 10.428571 9.742857 9.753571 9.851429
## 6 9.857143 8.928571 9.664286 9.580952 9.325000 9.674286
## temp.5.week temp.6.week season_numeric
## 1 9.347619 9.512245 201011
## 2 10.495238 10.593878 201112
## 3 9.723810 9.697959 201213
## 4 10.514286 10.048980 201314
## 5 9.861905 10.130612 201415
## 6 9.452381 9.287755 201011
# check dimensions of the dataset
dim(nests.per.week)
## [1] 158 23
# check median number of nests per week
median(nests.per.week$num.of.nests)
## [1] 2
# check standard deviation of number of nests per week
sd(nests.per.week$num.of.nests)
## [1] 2.228031
# check range of number of nests per week
range(nests.per.week$num.of.nests)
## [1] 1 11
Some preliminary anaylysis before modelling to check for model assumptions and standardization of predictor variables.
# check for outliers
boxplot(nests.per.week$num.of.nests ~ nests.per.week$breeding_season)
# check for homogeneitry of variances
with(nests.per.week, tapply(num.of.nests, breeding_season, FUN = var)) # upto 10
## 2010/11 2011/12 2012/13 2013/14 2014/15 2015/16 2017/18
## 5.575499 9.473684 5.177866 2.389493 1.474026 5.873518 3.842105
table(nests.per.week$breeding_season)
##
## 2010/11 2011/12 2012/13 2013/14 2014/15 2015/16 2017/18
## 27 20 23 24 22 23 19
# check for normality and zero inflation
hist(nests.per.week$num.of.nests, breaks = 30) ##no zero inflation
# standardize predictor variables
nests.per.week$z.temp.1.week <- scale(nests.per.week$temp.1.week)
nests.per.week$z.prec.1.week <- scale(nests.per.week$prec.1.week)
nests.per.week$z.temp.2.week <- scale(nests.per.week$temp.2.week)
nests.per.week$z.prec.2.week <- scale(nests.per.week$prec.2.week)
nests.per.week$z.temp.3.week <- scale(nests.per.week$temp.3.week)
nests.per.week$z.prec.3.week <- scale(nests.per.week$prec.3.week)
nests.per.week$z.temp.4.week <- scale(nests.per.week$temp.4.week)
nests.per.week$z.prec.4.week <- scale(nests.per.week$prec.4.week)
nests.per.week$z.temp.5.week <- scale(nests.per.week$temp.5.week)
nests.per.week$z.prec.5.week <- scale(nests.per.week$prec.5.week)
nests.per.week$z.temp.6.week <- scale(nests.per.week$temp.6.week)
nests.per.week$z.prec.6.week <- scale(nests.per.week$prec.6.week)
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.1.week", "prec.1.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.1.week + nests.per.week$prec.1.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (1 week) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot number of nests per week vs pre-laying period temperature
qplot(temp.1.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (1 week) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M1 <- gam(num.of.nests ~ s(temp.1.week) + s(prec.1.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M1) # seems to have curvature
# build a maximal poisson model
library(glmmTMB) # load required package : glmmTMB
mod1.1 <- glmmTMB(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ I(z.prec.1.week^2) + (1|season_numeric/week), family = "poisson" ,
data= nests.per.week)
summary(mod1.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## I(z.prec.1.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 647.7 669.2 -316.9 633.7 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.15581 0.3947
## season_numeric (Intercept) 0.01312 0.1145
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.13077 0.09627 11.746 <2e-16 ***
## z.temp.1.week 0.07171 0.06154 1.165 0.2439
## z.prec.1.week 0.07920 0.08375 0.946 0.3443
## I(z.temp.1.week^2) -0.13088 0.06358 -2.058 0.0395 *
## I(z.prec.1.week^2) -0.02207 0.02182 -1.011 0.3119
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic precipitation term
mod1.2 <- glmmTMB(num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod1.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 646.8 665.2 -317.4 634.8 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.15928 0.3991
## season_numeric (Intercept) 0.01303 0.1141
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.11299 0.09529 11.680 <2e-16 ***
## z.temp.1.week 0.06866 0.06167 1.113 0.2656
## z.prec.1.week 0.01763 0.06007 0.294 0.7691
## I(z.temp.1.week^2) -0.13500 0.06380 -2.116 0.0343 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove precipitation
mod1.3 <- glmmTMB(num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod1.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.9 660.2 -317.5 634.9 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.1599 0.3999
## season_numeric (Intercept) 0.0135 0.1162
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.11403 0.09561 11.652 <2e-16 ***
## z.temp.1.week 0.07189 0.06068 1.185 0.2361
## I(z.temp.1.week^2) -0.13656 0.06364 -2.146 0.0319 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term
mod1.4 <- glmmTMB(num.of.nests ~ z.temp.1.week
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod1.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.1.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 647.6 659.8 -319.8 639.6 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.17563 0.4191
## season_numeric (Intercept) 0.01036 0.1018
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.97981 0.07396 13.248 <2e-16 ***
## z.temp.1.week 0.08063 0.05968 1.351 0.177
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using AIC
anova(mod1.1, mod1.2, mod1.3, mod1.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod1.4: num.of.nests ~ z.temp.1.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod1.3: num.of.nests ~ z.temp.1.week + I(z.temp.1.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod1.2: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) + , zi=~0, disp=~1
## mod1.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod1.1: num.of.nests ~ z.temp.1.week + z.prec.1.week + I(z.temp.1.week^2) + , zi=~0, disp=~1
## mod1.1: I(z.prec.1.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod1.4 4 647.57 659.83 -319.79 639.57
## mod1.3 5 644.93 660.24 -317.46 634.93 4.6454 1 0.03114 *
## mod1.2 6 646.84 665.22 -317.42 634.84 0.0853 1 0.77021
## mod1.1 7 647.73 669.17 -316.86 633.73 1.1149 1 0.29102
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.2.week", "prec.2.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.2.week + nests.per.week$prec.2.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (2 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.2.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (2 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M2 <- gam(num.of.nests ~ s(temp.2.week) + s(prec.2.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M2) # some curvature
# build a maximal poisson model
mod2.1 <- glmmTMB(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ I(z.prec.2.week^2) + (1|season_numeric/week), family = "poisson" ,
data= nests.per.week)
summary(mod2.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## I(z.prec.2.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.9 666.4 -315.5 630.9 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.14388 0.3793
## season_numeric (Intercept) 0.01543 0.1242
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.16248 0.09963 11.668 <2e-16 ***
## z.temp.2.week 0.08805 0.06181 1.424 0.1543
## z.prec.2.week 0.10253 0.08511 1.205 0.2283
## I(z.temp.2.week^2) -0.14523 0.06677 -2.175 0.0296 *
## I(z.prec.2.week^2) -0.03943 0.02708 -1.456 0.1453
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic precipitation term
mod2.2 <- glmmTMB(num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod2.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 645.2 663.6 -316.6 633.2 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.15252 0.3905
## season_numeric (Intercept) 0.01472 0.1213
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.13166 0.09824 11.520 <2e-16 ***
## z.temp.2.week 0.08186 0.06215 1.317 0.1878
## z.prec.2.week 0.01410 0.06187 0.228 0.8197
## I(z.temp.2.week^2) -0.15390 0.06723 -2.289 0.0221 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove precipitation
mod2.3 <- glmmTMB(num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod2.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 643.3 658.6 -316.6 633.3 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.15256 0.3906
## season_numeric (Intercept) 0.01533 0.1238
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.13268 0.09856 11.492 <2e-16 ***
## z.temp.2.week 0.08499 0.06057 1.403 0.1605
## I(z.temp.2.week^2) -0.15523 0.06700 -2.317 0.0205 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term
mod2.4 <- glmmTMB(num.of.nests ~ z.temp.2.week + (1|season_numeric/week),
family = "poisson" , data= nests.per.week)
summary(mod2.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.2.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 646.7 658.9 -319.3 638.7 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.172960 0.41588
## season_numeric (Intercept) 0.009863 0.09931
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.97967 0.07336 13.354 <2e-16 ***
## z.temp.2.week 0.09889 0.05960 1.659 0.0971 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using AIC
anova(mod2.1, mod2.2, mod2.3, mod2.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod2.4: num.of.nests ~ z.temp.2.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod2.3: num.of.nests ~ z.temp.2.week + I(z.temp.2.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod2.2: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) + , zi=~0, disp=~1
## mod2.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod2.1: num.of.nests ~ z.temp.2.week + z.prec.2.week + I(z.temp.2.week^2) + , zi=~0, disp=~1
## mod2.1: I(z.prec.2.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod2.4 4 646.65 658.90 -319.33 638.65
## mod2.3 5 643.28 658.59 -316.64 633.28 5.3749 1 0.02043 *
## mod2.2 6 645.23 663.60 -316.61 633.23 0.0516 1 0.82036
## mod2.1 7 644.95 666.38 -315.47 630.95 2.2809 1 0.13098
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.3.week", "prec.3.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.3.week + nests.per.week$prec.3.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (3 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.3.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (3 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M3 <- gam(num.of.nests ~ s(temp.3.week) + s(prec.3.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M3) # some curvature
# build a maximal poisson model
mod3.1 <- glmmTMB(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ I(z.prec.3.week^2) + (1|season_numeric/week), family = "poisson" ,
data= nests.per.week)
summary(mod3.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## I(z.prec.3.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 642.8 664.2 -314.4 628.8 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.13278 0.3644
## season_numeric (Intercept) 0.01632 0.1277
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.16754 0.10231 11.411 <2e-16 ***
## z.temp.3.week 0.07154 0.06194 1.155 0.2481
## z.prec.3.week 0.19563 0.08770 2.231 0.0257 *
## I(z.temp.3.week^2) -0.13025 0.06985 -1.865 0.0622 .
## I(z.prec.3.week^2) -0.05658 0.02919 -1.938 0.0526 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic temperature term
mod3.2 <- glmmTMB(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.prec.3.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod3.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.prec.3.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.3 662.7 -316.1 632.3 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.14385 0.3793
## season_numeric (Intercept) 0.01263 0.1124
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.04399 0.07882 13.245 <2e-16 ***
## z.temp.3.week 0.08525 0.06081 1.402 0.1610
## z.prec.3.week 0.21722 0.08745 2.484 0.0130 *
## I(z.prec.3.week^2) -0.06098 0.02931 -2.081 0.0375 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove temperature
mod3.3 <- glmmTMB(num.of.nests ~ z.prec.3.week + I(z.prec.3.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod3.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.prec.3.week + I(z.prec.3.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.2 659.6 -317.1 634.2 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.1467 0.3830
## season_numeric (Intercept) 0.0167 0.1292
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.04305 0.08260 12.627 < 2e-16 ***
## z.prec.3.week 0.23837 0.08859 2.691 0.00713 **
## I(z.prec.3.week^2) -0.05857 0.02935 -1.996 0.04594 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term
mod3.4 <- glmmTMB(num.of.nests ~ z.prec.3.week
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod3.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.prec.3.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 646.3 658.6 -319.2 638.3 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.16658 0.4081
## season_numeric (Intercept) 0.01382 0.1175
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.98303 0.07685 12.791 <2e-16 ***
## z.prec.3.week 0.10706 0.06066 1.765 0.0776 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# run a null model
mod3.5 <- glmmTMB(num.of.nests ~ 1 + (1|season_numeric/week), family = "poisson",
data= nests.per.week)
# since in model 3.1 both the quadratic terms had comparable significance,
#remove quadratic term for precipitation and model again
mod3.6 <- glmmTMB(num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod3.6)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.7 663.0 -316.3 632.7 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.14992 0.3872
## season_numeric (Intercept) 0.01468 0.1212
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.12258 0.10089 11.126 <2e-16 ***
## z.temp.3.week 0.06333 0.06264 1.011 0.3119
## z.prec.3.week 0.07265 0.06175 1.177 0.2393
## I(z.temp.3.week^2) -0.14302 0.07090 -2.017 0.0437 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove precipitation
mod3.7 <- glmmTMB(num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod3.7)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 644.0 659.3 -317.0 634.0 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.15425 0.3927
## season_numeric (Intercept) 0.01614 0.1271
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.13082 0.10194 11.093 <2e-16 ***
## z.temp.3.week 0.08099 0.06062 1.336 0.182
## I(z.temp.3.week^2) -0.15295 0.07091 -2.157 0.031 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using anova
anova(mod3.1, mod3.2, mod3.3, mod3.4, mod3.5, mod3.6, mod3.7, test = "Chi")
## Data: nests.per.week
## Models:
## mod3.5: num.of.nests ~ 1 + (1 | season_numeric/week), zi=~0, disp=~1
## mod3.4: num.of.nests ~ z.prec.3.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod3.3: num.of.nests ~ z.prec.3.week + I(z.prec.3.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod3.7: num.of.nests ~ z.temp.3.week + I(z.temp.3.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod3.2: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.prec.3.week^2) + , zi=~0, disp=~1
## mod3.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod3.6: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) + , zi=~0, disp=~1
## mod3.6: (1 | season_numeric/week), zi=~0, disp=~1
## mod3.1: num.of.nests ~ z.temp.3.week + z.prec.3.week + I(z.temp.3.week^2) + , zi=~0, disp=~1
## mod3.1: I(z.prec.3.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod3.5 3 647.39 656.58 -320.70 641.39
## mod3.4 4 646.32 658.58 -319.16 638.32 3.0692 1 0.07979 .
## mod3.3 5 644.24 659.55 -317.12 634.24 4.0845 1 0.04328 *
## mod3.7 5 644.02 659.34 -317.01 634.02 0.2177 0 < 2e-16 ***
## mod3.2 6 644.29 662.66 -316.14 632.29 1.7362 1 0.18762
## mod3.6 6 644.67 663.04 -316.33 632.67 0.0000 0 1.00000
## mod3.1 7 642.80 664.23 -314.40 628.80 3.8731 1 0.04907 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.4.week", "prec.4.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.4.week + nests.per.week$prec.4.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (4 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.4.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (4 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M4 <- gam(num.of.nests ~ s(temp.4.week) + s(prec.4.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M4) # seems to have curvature
# build a maximal poisson model
mod4.1 <- glmmTMB(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ I(z.prec.4.week^2) + (1|season_numeric/week), family = "poisson" ,
data= nests.per.week)
summary(mod4.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) +
## I(z.prec.4.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 639.3 660.7 -312.7 625.3 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.11841 0.3441
## season_numeric (Intercept) 0.02071 0.1439
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.20509 0.10513 11.462 < 2e-16 ***
## z.temp.4.week 0.07061 0.06183 1.142 0.25341
## z.prec.4.week 0.24864 0.09401 2.645 0.00817 **
## I(z.temp.4.week^2) -0.12959 0.06991 -1.854 0.06380 .
## I(z.prec.4.week^2) -0.09545 0.03487 -2.737 0.00620 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic temperature term
mod4.2 <- glmmTMB(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.prec.4.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod4.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.prec.4.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 640.8 659.1 -314.4 628.8 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.12943 0.3598
## season_numeric (Intercept) 0.01682 0.1297
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.08400 0.08281 13.090 < 2e-16 ***
## z.temp.4.week 0.08158 0.06066 1.345 0.17868
## z.prec.4.week 0.27793 0.09321 2.982 0.00287 **
## I(z.prec.4.week^2) -0.10162 0.03475 -2.925 0.00345 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove temperature
mod4.3 <- glmmTMB(num.of.nests ~ z.prec.4.week + I(z.prec.4.week^2)
+ (1|season_numeric/week), family = "poisson" , data= nests.per.week)
summary(mod4.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.prec.4.week + I(z.prec.4.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 640.6 655.9 -315.3 630.6 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.13204 0.3634
## season_numeric (Intercept) 0.02073 0.1440
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.08303 0.08632 12.546 < 2e-16 ***
## z.prec.4.week 0.30322 0.09411 3.222 0.00127 **
## I(z.prec.4.week^2) -0.09931 0.03487 -2.848 0.00440 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic precipitation term
mod4.4 <- glmmTMB(num.of.nests ~ z.prec.4.week + (1|season_numeric/week),
family = "poisson" , data= nests.per.week)
summary(mod4.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.prec.4.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 646.9 659.2 -319.5 638.9 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.16926 0.4114
## season_numeric (Intercept) 0.01332 0.1154
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.98265 0.07650 12.844 <2e-16 ***
## z.prec.4.week 0.09800 0.06215 1.577 0.115
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using AIC
anova(mod4.1, mod4.2, mod4.3, mod4.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod4.4: num.of.nests ~ z.prec.4.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod4.3: num.of.nests ~ z.prec.4.week + I(z.prec.4.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod4.2: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.prec.4.week^2) + , zi=~0, disp=~1
## mod4.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod4.1: num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2) + , zi=~0, disp=~1
## mod4.1: I(z.prec.4.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod4.4 4 646.93 659.18 -319.46 638.93
## mod4.3 5 640.55 655.87 -315.28 630.55 8.3721 1 0.00381 **
## mod4.2 6 640.76 659.13 -314.38 628.76 1.7953 1 0.18028
## mod4.1 7 639.31 660.74 -312.65 625.31 3.4526 1 0.06315 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.5.week", "prec.5.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.5.week + nests.per.week$prec.5.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (5 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.5.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (5 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M5 <- gam(num.of.nests ~ s(temp.5.week) + s(prec.5.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M5) # only temperature has curvature
# build a maximal poisson model
mod5.1 <- glmmTMB(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2)
+ I(z.prec.5.week^2) + (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod5.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) +
## I(z.prec.5.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 640.2 661.7 -313.1 626.2 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.12012 0.3466
## season_numeric (Intercept) 0.02445 0.1564
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.23302 0.10904 11.308 <2e-16 ***
## z.temp.5.week 0.06003 0.06284 0.955 0.3395
## z.prec.5.week 0.20642 0.09740 2.119 0.0341 *
## I(z.temp.5.week^2) -0.16585 0.07204 -2.302 0.0213 *
## I(z.prec.5.week^2) -0.08879 0.03673 -2.418 0.0156 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term for temperature
mod5.2 <- glmmTMB(num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.prec.5.week^2)
+ (1|season_numeric/week), family = "poisson", data= nests.per.week)
summary(mod5.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.prec.5.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 643.5 661.9 -315.7 631.5 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.13917 0.3731
## season_numeric (Intercept) 0.01751 0.1323
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.07685 0.08440 12.759 < 2e-16 ***
## z.temp.5.week 0.06980 0.06179 1.130 0.25862
## z.prec.5.week 0.23959 0.09664 2.479 0.01317 *
## I(z.prec.5.week^2) -0.09586 0.03625 -2.645 0.00818 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove temperature term
mod5.3 <- glmmTMB(num.of.nests ~ z.prec.5.week + I(z.prec.5.week^2)
+ (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod5.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.prec.5.week + I(z.prec.5.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 642.8 658.1 -316.4 632.8 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.14115 0.3757
## season_numeric (Intercept) 0.02031 0.1425
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.07719 0.08691 12.395 < 2e-16 ***
## z.prec.5.week 0.26647 0.09623 2.769 0.00562 **
## I(z.prec.5.week^2) -0.09510 0.03640 -2.613 0.00899 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term
mod5.4 <- glmmTMB(num.of.nests ~ z.prec.5.week + (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod5.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.prec.5.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 648.1 660.3 -320.0 640.1 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.17534 0.4187
## season_numeric (Intercept) 0.01168 0.1081
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.98166 0.07523 13.049 <2e-16 ***
## z.prec.5.week 0.07134 0.06229 1.145 0.252
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using AIC
anova(mod5.1, mod5.2, mod5.3, mod5.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod5.4: num.of.nests ~ z.prec.5.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod5.3: num.of.nests ~ z.prec.5.week + I(z.prec.5.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod5.2: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.prec.5.week^2) + , zi=~0, disp=~1
## mod5.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod5.1: num.of.nests ~ z.temp.5.week + z.prec.5.week + I(z.temp.5.week^2) + , zi=~0, disp=~1
## mod5.1: I(z.prec.5.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod5.4 4 648.10 660.35 -320.05 640.10
## mod5.3 5 642.76 658.07 -316.38 632.76 7.3410 1 0.00674 **
## mod5.2 6 643.49 661.87 -315.74 631.49 1.2670 1 0.26033
## mod5.1 7 640.21 661.65 -313.11 626.21 5.2749 1 0.02164 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Inspect relationships and check for multiocollinearity.
# subset covariates of interest to check for multicollinearity
covariates <- nests.per.week[, c("num.of.nests", "temp.6.week", "prec.6.week")]
# plot the relationships
pairs.panels(covariates)
# visually inspect relationships
pairs(nests.per.week$num.of.nests ~ nests.per.week$temp.6.week + nests.per.week$prec.6.week)
# visually inspect relationships by each year
# plot number of nests per week vs pre-laying period precipitation
qplot(prec.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period precipitation",
xlab = "Prelaying period (6 weeks) precipitation mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# plot for number of nests per week vs pre-laying period temperature
qplot(temp.6.week, num.of.nests, data = nests.per.week, geom = "point",
main = "Number of nests per week vs Pre-laying period temperature",
xlab = "Prelaying period (6 weeks) temperature mean",
ylab = "Number of nests per week") +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
Model the relationships and reduce models using step-wise deletion.
# run a GAM to see if there is any curvature
M6 <- gam(num.of.nests ~ s(temp.6.week) + s(prec.6.week), data = nests.per.week)
par(mfrow = c(1, 2))
plot(M6) # some curvature
# build a maximal poisson model
mod6.1 <- glmmTMB(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ I(z.prec.6.week^2) + (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod6.1)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## I(z.prec.6.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 640.7 662.2 -313.4 626.7 151
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.12285 0.3505
## season_numeric (Intercept) 0.02485 0.1576
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.24073 0.10994 11.286 <2e-16 ***
## z.temp.6.week 0.05344 0.06407 0.834 0.4042
## z.prec.6.week 0.21359 0.10062 2.123 0.0338 *
## I(z.temp.6.week^2) -0.18253 0.07211 -2.531 0.0114 *
## I(z.prec.6.week^2) -0.07956 0.03713 -2.143 0.0322 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term for precipitation
mod6.2 <- glmmTMB(num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2)
+ (1|season_numeric/week), family = "poisson", data= nests.per.week)
summary(mod6.2)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) +
## (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 643.6 662.0 -315.8 631.6 152
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.14505 0.3809
## season_numeric (Intercept) 0.01766 0.1329
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.17664 0.10335 11.385 < 2e-16 ***
## z.temp.6.week 0.05214 0.06482 0.804 0.42122
## z.prec.6.week 0.05401 0.06732 0.802 0.42236
## I(z.temp.6.week^2) -0.19784 0.07329 -2.699 0.00695 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove precipitation term
mod6.3 <- glmmTMB(num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2)
+ (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod6.3)
## Family: poisson ( log )
## Formula:
## num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 642.2 657.5 -316.1 632.2 153
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.1462 0.3823
## season_numeric (Intercept) 0.0196 0.1400
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.17942 0.10473 11.262 <2e-16 ***
## z.temp.6.week 0.07150 0.05984 1.195 0.232
## I(z.temp.6.week^2) -0.20161 0.07337 -2.748 0.006 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# remove quadratic term
mod6.4 <- glmmTMB(num.of.nests ~ z.temp.6.week + (1|season_numeric/week), family = "poisson",
data= nests.per.week)
summary(mod6.4)
## Family: poisson ( log )
## Formula: num.of.nests ~ z.temp.6.week + (1 | season_numeric/week)
## Data: nests.per.week
##
## AIC BIC logLik deviance df.resid
## 647.7 659.9 -319.8 639.7 154
##
## Random effects:
##
## Conditional model:
## Groups Name Variance Std.Dev.
## week:season_numeric (Intercept) 0.176054 0.41959
## season_numeric (Intercept) 0.009697 0.09847
## Number of obs: 158, groups: week:season_numeric, 158; season_numeric, 7
##
## Conditional model:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.98005 0.07333 13.366 <2e-16 ***
## z.temp.6.week 0.07826 0.05957 1.314 0.189
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# compare models using AIC
anova(mod6.1, mod6.2, mod6.3, mod6.4, test = "Chi")
## Data: nests.per.week
## Models:
## mod6.4: num.of.nests ~ z.temp.6.week + (1 | season_numeric/week), zi=~0, disp=~1
## mod6.3: num.of.nests ~ z.temp.6.week + I(z.temp.6.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## mod6.2: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) + , zi=~0, disp=~1
## mod6.2: (1 | season_numeric/week), zi=~0, disp=~1
## mod6.1: num.of.nests ~ z.temp.6.week + z.prec.6.week + I(z.temp.6.week^2) + , zi=~0, disp=~1
## mod6.1: I(z.prec.6.week^2) + (1 | season_numeric/week), zi=~0, disp=~1
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## mod6.4 4 647.67 659.92 -319.84 639.67
## mod6.3 5 642.23 657.54 -316.11 632.23 7.4483 1 0.00635 **
## mod6.2 6 643.60 661.97 -315.80 631.60 0.6307 1 0.42711
## mod6.1 7 640.73 662.17 -313.37 626.73 4.8621 1 0.02745 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Compare all the models to choose the best model. Also compare final model with a model without random effects to check for their importance. Check for model overdispersion.
# compare best models from different analyses
AIC(mod1.3, mod2.3, mod3.7, mod3.1, mod4.3, mod4.1, mod5.1, mod6.1)
## df AIC
## mod1.3 5 644.9294
## mod2.3 5 643.2788
## mod3.7 5 644.0226
## mod3.1 7 642.7959
## mod4.3 5 640.5539
## mod4.1 7 639.3059
## mod5.1 7 640.2147
## mod6.1 7 640.7334
# comparing best model with and without random effects
# run a model without random effects
M <- glm(num.of.nests ~ z.temp.4.week + z.prec.4.week + I(z.temp.4.week^2)
+ I(z.prec.4.week^2), family = "poisson" , data= nests.per.week)
# compare the two models
AIC(mod4.1, M)
## df AIC
## mod4.1 7 639.3059
## M 5 650.5464
Plot model results for the best model
# install package strengejacke for sjPlot to work properly
devtools::install_github("strengejacke/strengejacke", force = TRUE)
## Downloading GitHub repo strengejacke/strengejacke@master
## from URL https://api.github.com/repos/strengejacke/strengejacke/zipball/master
## Installing strengejacke
## '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file \
## --no-environ --no-save --no-restore --quiet CMD INSTALL \
## '/private/var/folders/_g/g0tfx7tn3qv6f6qtzkppkxd00000gn/T/RtmpVKY5fh/devtools2bc05306ad62/strengejacke-strengejacke-d41d521' \
## --library='/Library/Frameworks/R.framework/Versions/3.5/Resources/library' \
## --install-tests
##
# plot for effect of precipitation
p1 <- plot_model(mod4.1, type = "pred", terms = "z.prec.4.week",
axis.title = c("Pre-laying precipitation average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "blue")
## Warning in checkTerms(data.tmb1$terms, data.tmb0$terms): Predicting new random effect levels for terms: 1 | week:season_numeric
## Disable this warning with 'allow.new.levels=TRUE'
# plot for effect of temperature
p2 <- plot_model(mod4.1, type = "pred", terms = "z.temp.4.week",
axis.title = c("Pre-laying temperature average (z-standardized)",
"Number of nests per week"),
title = "") + theme_bw() + geom_line(colour = "red")
## Warning in checkTerms(data.tmb1$terms, data.tmb0$terms): Predicting new random effect levels for terms: 1 | week:season_numeric
## Disable this warning with 'allow.new.levels=TRUE'
# arrange the plots together
grid.arrange(p1, p2, nrow = 1, ncol = 2)
####### Temperature #######
# plotting pre-laying temperature by years on the same plot
p <- ggplot(nests.per.week, aes(x=week, y=temp.4.week, col = breeding_season,
group=breeding_season))
p + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average temperature for laying period by year and save as data frame
tmean <- as.data.frame(with(nests.per.week, tapply(temp.4.week, season_numeric, FUN = mean)))
# create a new column
tmean$breeding_year <- as.numeric(seq(1,7, by = 1))
# change name of second column
colnames(tmean)[1] <- "tmean"
# check data frame
head(tmean)
## tmean breeding_year
## 201011 14.39304 1
## 201112 15.68214 2
## 201213 15.10882 3
## 201314 14.99052 4
## 201415 15.08948 5
## 201516 15.80596 6
####### Precipitation ########
# plot pre-laying period precipitation
P <- ggplot(nests.per.week, aes(x=week, y=prec.4.week, col = breeding_season,
group=breeding_season))
P + geom_point(pch = 18) + theme_bw()
### averaged across years
# compute average precipitation for laying period by year and save as data frame
pmean <- as.data.frame(with(nests.per.week,
tapply(prec.4.week, season_numeric, FUN = mean, na.rm = T)))
# create a new column
pmean$breeding_year <- as.numeric(seq(1,7, by = 1))
# check data frame
head(pmean)
## with(nests.per.week, tapply(prec.4.week, season_numeric, FUN = mean,
## 201011 3.1797672
## 201112 1.8214286
## 201213 0.8929193
## 201314 1.4004762
## 201415 1.1470130
## 201516 0.9561905
## breeding_year
## 201011 1
## 201112 2
## 201213 3
## 201314 4
## 201415 5
## 201516 6
# change name of second column
colnames(pmean)[1] <- "pmean"
# check data frame
head(pmean)
## pmean breeding_year
## 201011 3.1797672 1
## 201112 1.8214286 2
## 201213 0.8929193 3
## 201314 1.4004762 4
## 201415 1.1470130 5
## 201516 0.9561905 6
# plots for climatic trends at Cheetham
# set page layout to incorporate two plots side by side
par(mfrow = c(1,2))
# set the axis lables to be slightly bigger
par(cex.lab=1.4)
# plot annual mean temperature for the laying period vs year
plot(tmean$tmean ~ tmean$breeding_year,
main="Annual breeding season mean temperature (Cheetham)",
ylab="Temperature (Degree celsius)", xlab=" Breeding year",
pch = 18, cex = 2, col = "red", xaxt = "n", cex.main = 1)
# costumize x axis ticks
axis(1, at = c(1,2, 3, 4, 5, 6, 7),
labels = c("2010/11", "2011/12", "2012/13", "2013/14",
"2014/15", "2015/16", "2017/18"))
# plot annual mean precipitation for the laying period vs year
plot(pmean$pmean ~ pmean$breeding_year,
main="Annual breeding season mean precipitation (Cheetham)",
ylab="Precipitation (mm)", xlab="Breeding year",
pch = 18, cex = 2, col = "blue", xaxt = "n", cex.main = 1)
# costumize x axis ticks
axis(1, at = c(1,2, 3, 4, 5, 6, 7),
labels = c("2010/11", "2011/12", "2012/13", "2013/14",
"2014/15", "2015/16", "2017/18"))
###### correlation test for climate data #######
# test for correlation between year and temperature means using spearman's correlation test
cor.test(tmean$tmean, tmean$breeding_year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: tmean$tmean and tmean$breeding_year
## S = 18, p-value = 0.1095
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.6785714
# test for correlation between year and precipitation meansusing spearman's correlation test
cor.test(pmean$pmean, pmean$breeding_year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: pmean$pmean and pmean$breeding_year
## S = 80, p-value = 0.3536
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.4285714
# load datafile for galicia
galicia <- read.csv("../Data/Galicia_final.csv", header = T, as.is = T)
head(galicia)
## X species population year site nest Nest_ID easting northing
## 1 1 C.alexandrinus Galicia 2017 CI 2 CI0217 508066 4674365
## 2 2 C.alexandrinus Galicia 2017 CI 3 CI0317 508031 4674687
## 3 3 C.alexandrinus Galicia 2017 CI 4 CI0417 508010 4674374
## 4 4 C.alexandrinus Galicia 2017 CI 9 CI0917 508044 4674504
## 5 5 C.alexandrinus Galicia 2017 BD 9 BD0917 524649 4793256
## 6 6 C.alexandrinus Galicia 2017 BD 14 BD1417 524513 4793268
## clutch_initiation laying_date hatching_date clutch_size fate
## 1 2017-04-09 2017-04-12 NA 3 predated
## 2 2017-04-07 2017-04-10 NA 3 predated
## 3 2017-04-25 2017-04-28 NA 3 predated
## 4 2017-06-01 2017-06-04 NA 3 predated
## 5 2017-05-09 2017-05-12 NA 3 predated
## 6 2017-05-17 2017-05-20 NA 3 predated
## no_chicks Nr_chicks_fledged length1 breadth1 weight1 length2 breadth2
## 1 0 NA 31.44 23.35 8.8 31.67 23.01
## 2 0 NA 33.23 23.44 8.9 32.50 23.38
## 3 0 NA 32.62 23.99 9.6 32.91 23.95
## 4 0 NA 32.85 23.73 9.2 32.70 24.01
## 5 0 NA 30.52 22.54 7.5 30.62 22.29
## 6 0 NA 32.47 23.14 9.0 31.41 23.29
## weight2 length3 breadth3 weight3 breeding.success clutch_volume
## 1 8.6 31.21 23.52 8.8 0 24.87099
## 2 8.7 32.02 22.59 8.0 0 25.44844
## 3 9.6 32.96 24.04 9.6 0 27.55572
## 4 9.5 33.16 23.96 9.6 0 27.40345
## 5 7.5 30.74 22.30 7.3 0 22.35882
## 6 8.7 30.93 23.18 8.4 0 24.80689
## predation_binary LD_julian_day month CI_julian_day layingday_tmean
## 1 1 102 4 99 19.85
## 2 1 100 4 97 18.50
## 3 1 118 4 115 15.65
## 4 1 155 6 152 22.25
## 5 1 132 5 129 18.85
## 6 1 140 5 137 21.45
## prelaying_tmean laying_tmean layingday_prec prelaying_prec_mean
## 1 15.18833 18.80000 0.0 1.9933333
## 2 15.28500 17.71667 0.0 1.9933333
## 3 17.95000 16.26667 48.8 0.0000000
## 4 21.03333 23.60000 0.0 0.5133333
## 5 18.37000 18.33333 0.6 4.3933333
## 6 18.83500 20.76667 0.0 4.8800000
## laying_prec_mean
## 1 0.000000
## 2 0.000000
## 3 43.733333
## 4 0.000000
## 5 4.866667
## 6 0.000000
# load datafile for Hungary
hungary <- read.csv("../Data/Hungary_final_unkincl.csv", header = T, as.is = T)
head(hungary)
## X year site nest TYPE found_date laying_date INCUB.START SINCT
## 1 24 1989 8 1 0 627 1989-06-21 621 3
## 2 25 1989 8 2 0 629 1989-06-07 607 3
## 3 26 1989 8 4 0 629 1989-06-13 613 3
## 4 40 1990 7 20 4 430 1990-04-30 430 1
## 5 41 1990 7 22 3 512 1990-04-30 504 1
## 6 42 1990 7 24 2 502 1990-05-02 502 1
## END.DATE.OF.INCUB EGG.ORDER fate no_chicks FRESH length1 breadth1
## 1 716 -1 15 3 0 32.2 22.9
## 2 702 -1 15 3 0 31.9 23.3
## 3 708 -1 15 1 0 30.6 22.5
## 4 526 -1 15 4 1 33.1 23.5
## 5 530 -1 15 3 0 33.6 23.9
## 6 528 -1 15 2 1 31.6 23.3
## weight1 length2 breadth2 weight2 length3 breadth3 weight3 FAM MADATE
## 1 8.6 32.2 22.6 8.8 32.1 22.6 8.9 2801 118
## 2 8.3 33.0 23.4 8.4 32.0 23.4 8.6 2802 120
## 3 7.7 29.6 22.3 7.6 30.3 22.3 8.1 2804 120
## 4 9.0 31.4 23.1 8.2 31.9 23.1 8.3 3720 60
## 5 9.5 32.8 24.1 9.5 33.6 24.1 10.1 3722 72
## 6 8.9 32.0 22.6 8.8 30.6 22.6 8.5 3724 62
## MASINC MAEINC MACOMPL VOL1 VOL2 VOL3 VOL WEI clutch_size NEWSITE CORD
## 1 112 137.00 112 8.21 7.99 8.25 8.15 8.77 3 1 1
## 2 98 123.00 98 8.42 8.78 8.88 8.69 8.43 3 1 -1
## 3 104 129.00 104 7.53 7.15 7.66 7.45 7.78 3 1 -1
## 4 60 86.00 60 8.88 8.14 8.27 8.43 8.47 3 2 1
## 5 64 90.00 60 9.33 9.26 9.88 9.49 9.67 3 2 1
## 6 62 88.00 62 8.34 7.94 7.80 8.03 8.72 3 2 1
## LD_julian_day month week clutch_initiation CI_julian_day
## 1 172 6 25 1989-06-18 172
## 2 158 6 23 1989-06-04 158
## 3 164 6 24 1989-06-10 164
## 4 120 4 17 1990-04-27 120
## 5 120 4 17 1990-04-27 120
## 6 122 5 17 1990-04-29 122
## breeding_success clutch_volume predation_binary sites species
## 1 1.0000000 24.16774 0 2 C.alexandrinus
## 2 1.0000000 25.71406 0 2 C.alexandrinus
## 3 0.3333333 22.00556 0 2 C.alexandrinus
## 4 1.3333333 25.29970 0 2 C.alexandrinus
## 5 1.0000000 28.07060 0 2 C.alexandrinus
## 6 0.6666667 23.87665 0 2 C.alexandrinus
## population nestID Male Female layingday_tmean prelaying_tmean
## 1 Hungary 1989 1 8 3171 2150 15.40 17.27500
## 2 Hungary 1989 2 8 3155 NA 17.10 16.95500
## 3 Hungary 1989 4 8 3170 NA 17.80 17.85000
## 4 Hungary 1990 20 7 3196 2189 12.05 11.16500
## 5 Hungary 1990 22 7 3205 2192 12.05 11.16500
## 6 Hungary 1990 24 7 3224 2197 17.40 11.24333
## laying_tmean layingday_prec prelaying_prec_mean laying_prec_mean
## 1 17.60000 9.3 2.6566667 3.43333333
## 2 18.01667 0.0 2.2833333 4.80000000
## 3 17.31667 11.0 2.5466667 4.46666667
## 4 11.00000 0.0 0.6266667 0.06666667
## 5 11.00000 0.0 0.6266667 0.06666667
## 6 13.83333 0.0 0.5366667 0.00000000
# load datafile for cheetham
cheetham <- read.csv("../Data/Cheetham_final.csv", header = T, as.is = T)
head(cheetham)
## X breeding_season site species nest easting northing
## 1 1 2010/11 Cheetham RCP 2010CN01 305051 5802524
## 2 2 2010/11 Cheetham RCP 2010CN05 305624 5201725
## 3 3 2010/11 Cheetham RCP 2010CN14 305202 5802679
## 4 4 2010/11 Cheetham RCP 2010CN09 305175 5802698
## 5 5 2010/11 Cheetham RCP 2010CN17 304888 5802582
## 6 6 2010/11 RAAF Airbase RCP 2010RAAF03 303977 5800376
## found_date laying_date end_date fate no_chicks clutch_size length1
## 1 29/07/10 2010-07-27 27/08/10 Abandoned 0 2 28.00
## 2 27/08/10 2010-08-23 03/09/10 PRED 0 2 30.58
## 3 10/09/10 2010-09-06 - HATCH 2 2 30.20
## 4 23/09/10 2010-09-17 30/09/10 PRED 0 2 30.50
## 5 23/09/10 2010-09-19 - HATCH 2 2 29.21
## 6 30/09/10 2010-09-20 13/10/10 PRED 0 2 29.85
## breadth1 weight1 float1 length2 breadth2 weight2 float2 length3 breadth3
## 1 22.90 7.8 45 29.80 22.80 8.0 0 NA NA
## 2 22.51 9.0 30 31.35 23.29 9.0 45 NA NA
## 3 22.26 7.8 30 29.69 22.31 7.8 30 NA NA
## 4 21.78 7.5 45 30.02 21.15 6.0 45 NA NA
## 5 21.91 7.2 30 30.05 22.21 7.7 30 NA NA
## 6 22.32 7.5 90b 30.18 22.51 7.5 90b NA NA
## weight3 float3 observer comments zone Egg1.laying.date Egg2.laying.date
## 1 NA NA KE un-caged NA 23/07/10 27/07/10
## 2 NA NA KE un-caged NA 23/08/10 21/08/10
## 3 NA NA KE un-caged NA 06/09/10 06/09/10
## 4 NA NA KE un-caged NA 17/09/10 17/09/10
## 5 NA NA KE un-caged NA 19/09/10 19/09/10
## 6 NA NA KE un-caged NA 20/09/10 18/09/10
## Male Female breeding.success clutch_volume ref_date LD_julian_day
## 1 SX XA 0 14.66491 2010-06-01 56
## 2 PN RM 0 15.79494 2010-06-01 83
## 3 WA WC 1 14.45467 2010-06-01 97
## 4 WB YA 0 13.55787 2010-06-01 108
## 5 NE SY 1 14.01886 2010-06-01 110
## 6 SP Unb. 0 14.65920 2010-06-01 111
## laying_date_m ref_date_m month population layingday_tmean
## 1 7 6 1 Cheetham 8.8
## 2 8 6 2 Cheetham 11.5
## 3 9 6 3 Cheetham 8.8
## 4 9 6 3 Cheetham 11.3
## 5 9 6 3 Cheetham 11.1
## 6 9 6 3 Cheetham 12.2
## prelaying_tmean laying_tmean layingday_prec prelaying_prec_mean
## 1 9.090000 7.75 0.0 0.8666667
## 2 9.593333 10.85 0.2 2.0000000
## 3 9.760000 9.60 0.6 2.3800000
## 4 10.300000 10.60 0.0 1.7666667
## 5 10.346667 10.85 1.0 1.7600000
## 6 10.403333 11.65 0.0 1.7600000
## laying_prec_mean season_numeric
## 1 0.0 201011
## 2 0.2 201011
## 3 1.2 201011
## 4 0.0 201011
## 5 0.5 201011
## 6 0.5 201011
# load datafile for Samouco
samouco <- read.csv("../Data/Samouco_final.csv", header = T, as.is = T)
head(samouco)
## X nest LAT LONG year laying_date clutch_size fate
## 1 1 2 38°44'7.72"N 8°59'41.68"W 2005 2005-05-07 3 U
## 2 2 5 38°44'3.70"N 9° 0'1.74"W 2005 2005-04-24 3 H
## 3 3 6 38°44'6.03"N 8°59'43.41"W 2005 2005-05-05 3 H
## 4 4 39 38°44'12.77"N 8°59'43.34"W 2005 2005-06-27 3 H
## 5 5 40 38°44'10.56"N 8°59'45.70"W 2005 2005-05-14 3 H
## 6 6 96 38°44'18.58"N 8°59'36.96"W 2005 2005-05-21 3 U
## no_chicks EGG.L1 EGG.W1 EGG.L2 EGG.W2 EGG.L3 EGG.W3 LD_julian_day week
## 1 NA NA NA NA NA NA NA 127 18
## 2 2 NA NA NA NA NA NA 114 17
## 3 3 31.0 23.8 31.7 23.5 32.1 23.7 125 18
## 4 3 31.5 22.7 31.0 22.5 32.3 22.6 178 26
## 5 2 NA NA NA NA NA NA 134 19
## 6 NA NA NA NA NA NA NA 141 20
## species population
## 1 C.alexandrinus Samouco
## 2 C.alexandrinus Samouco
## 3 C.alexandrinus Samouco
## 4 C.alexandrinus Samouco
## 5 C.alexandrinus Samouco
## 6 C.alexandrinus Samouco
# set page layout to incorporate plots from all populations
par(mfrow = c(2,2))
# set the axis labels to be slightly bigger
par(cex.lab = 1.4)
## Hungary
# create a data frame with median laying dates for each study year
medianLD <- as.data.frame(with(hungary, tapply(LD_julian_day, year, FUN = median)))
# convert rownames to column names
medianLD <- tibble::rownames_to_column(medianLD, "year")
# rename column two to LD
colnames(medianLD)[2] <- "LD"
# check structure of data frame
str(medianLD)
## 'data.frame': 6 obs. of 2 variables:
## $ year: chr "1989" "1990" "1991" "1992" ...
## $ LD : num [1:6(1d)] 164 132 127 113 125 ...
# convert year to numeric
medianLD$year <- as.numeric(medianLD$year)
# plot the graph
plot(medianLD$LD ~ medianLD$year, pch = 18, col = "black",
xlab = "", ylab = "Median laying date",
main = "Hungary (n = 6)",
xaxt="n", yaxt = "n", cex = 2)
# customize x axis ticks, set them to be vertical
axis(1, at = seq(1988, 1994, by = 1), las=2)
# customize y axis ticks, set them to be horizontal
axis(2, at = seq(110, 160, by = 2), las = 1)
# add x axis label at some distance from the x axis
mtext(side=1, text="Year", line=4)
# check correlation between median laying dates and year
cor.test(medianLD$LD, medianLD$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: medianLD$LD and medianLD$year
## S = 62, p-value = 0.1028
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.7714286
## Galicia
# create a data frame with median laying dates for each study year
medianLD <- as.data.frame(with(galicia, tapply(LD_julian_day, year, FUN = median)))
# convert rownames to column names
medianLD <- tibble::rownames_to_column(medianLD, "year")
# rename column two to LD
colnames(medianLD)[2] <- "LD"
# check structure of data frame
str(medianLD)
## 'data.frame': 16 obs. of 2 variables:
## $ year: chr "1998" "1999" "2001" "2002" ...
## $ LD : num [1:16(1d)] 133 146 148 140 133 ...
# convert year to numeric
medianLD$year <- as.numeric(medianLD$year)
# plot the graph
plot(medianLD$LD ~ medianLD$year, pch = 18, col = "blue",
xlab = "", ylab = "Median laying date",
main = "Galicia (n = 16)",
xaxt="n", yaxt = "n", cex = 2)
# customize x axis ticks, set them to be vertical
axis(1, at = seq(1998, 2017, by = 1), las=2)
# customize y axis ticks, set them to be horizontal
axis(2, at = seq(115, 150, by = 2), las = 1)
# add x axis label at some distance from the x axis
mtext(side=1, text="Year", line=4)
# check correlation between median laying dates and year
cor.test(medianLD$LD, medianLD$year, method = "spearman")
## Warning in cor.test.default(medianLD$LD, medianLD$year, method =
## "spearman"): Cannot compute exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: medianLD$LD and medianLD$year
## S = 959.62, p-value = 0.1136
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## -0.4112022
## Samouco
# create a data frame with median laying dates for each study year
medianLD <- as.data.frame(with(samouco, tapply(LD_julian_day, year, FUN = median)))
# convert rownames to column names
medianLD <- tibble::rownames_to_column(medianLD, "year")
# rename column two to LD
colnames(medianLD)[2] <- "LD"
# check structure of data frame
str(medianLD)
## 'data.frame': 9 obs. of 2 variables:
## $ year: chr "2005" "2006" "2007" "2010" ...
## $ LD : num [1:9(1d)] 143 139 142 130 141 ...
# convert year to numeric
medianLD$year <- as.numeric(medianLD$year)
# plot the graph
plot(medianLD$LD ~ medianLD$year, pch = 18, col = "green",
xlab = "", ylab = "Median laying date",
main = "Samouco (n = 9)",
xaxt="n", yaxt = "n", cex = 2)
# customize x axis ticks, set them to be vertical
axis(1, at = seq(2006, 2017, by = 1), las=2)
# customize y axis ticks, set them to be horizontal
axis(2, at = seq(130, 180, by = 2), las = 1)
# add x axis label at some distance from the x axis
mtext(side=1, text="Year", line=4)
# check correlation between median laying dates and year
cor.test(medianLD$LD, medianLD$year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: medianLD$LD and medianLD$year
## S = 36, p-value = 0.04325
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.7
## Cheetham
# create a data frame with median laying dates for each study year
medianLD <- as.data.frame(with(cheetham, tapply(LD_julian_day, season_numeric, FUN = median)))
medianLD$breeding_year <- seq(1,7, by = 1)
# rename column one to LD
colnames(medianLD)[1] <- "LD"
# check structure of data frame
str(medianLD)
## 'data.frame': 7 obs. of 2 variables:
## $ LD : int [1:7(1d)] 152 200 189 181 165 168 198
## $ breeding_year: num 1 2 3 4 5 6 7
# plot the graph
plot(medianLD$LD ~ medianLD$breeding_year, pch = 18, col = "red",
xlab = "", ylab = "Median laying date",
main = "Cheetham (n= 7)",
xaxt="n", yaxt = "n", cex = 2)
# customize x axis ticks, set them to be vertical
axis(1, at = c(1,2, 3, 4, 5, 6, 7),
labels = c("2010/11", "2011/12", "2012/13", "2013/14",
"2014/15", "2015/16", "2017/18"), las = 2, cex.axis = 1)
# customize y axis ticks, set them to be horizontal
axis(2, at = seq(150, 200, by = 2), las = 1)
# add x axis label at some distance from the x axis
mtext(side=1, text="Breeding year", line=4)
# check correlation between median laying dates and year
cor.test(medianLD$LD, medianLD$breeding_year, method = "spearman")
##
## Spearman's rank correlation rho
##
## data: medianLD$LD and medianLD$breeding_year
## S = 48, p-value = 0.7825
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.1428571
# check if there are any missing values for clutch volume
table(is.na(hungary$clutch_volume)) # no missing values
##
## FALSE
## 181
# basic descriptive stats for clutch volume
dim(hungary)
## [1] 181 56
mean(hungary$clutch_volume)
## [1] 24.31281
sd(hungary$clutch_volume)
## [1] 2.712327
range(hungary$clutch_volume)
## [1] 9.155462 28.457940
# check for normality, zero inflation and outliers
hist(hungary$clutch_volume) # outliers seem to be biologically valid
# subset data to remove outliers
outliers.rem <- subset(hungary, hungary$clutch_volume > 10)
# plot again
hist(outliers.rem$clutch_volume) # normal distribution
# subset covariates of interest to check for multicollinearity
covariates <- hungary[, c("clutch_volume", "LD_julian_day", "year")]
# plot the relations
pairs.panels(covariates)
# visually inspect relationships by each year
# plot for clutch volume vs laying date
qplot(LD_julian_day, clutch_volume, data = hungary, geom = "point",
xlab = "Laying date (Julian days)", ylab = "Clutch volume (cm3)",
main = "Clutch volume vs Laying date") + theme_classic() +
facet_wrap(~year, ncol=3, nrow=2) + theme_bw()
# visually inspect relationship between clutch volume and year
boxplot(hungary$clutch_volume ~ hungary$year, xlab = "Year",
ylab = "Clutch volume (cm3)", main = "Clutch volume vs Year")
# modeling
# run a GAM to see if there is any curvature
M <- gam(hungary$clutch_volume ~ s(hungary$LD_julian_day))
plot(M)
# build a maximal model
m1 <- lm(hungary$clutch_volume ~ hungary$LD_julian_day +
I(hungary$LD_julian_day^2) + hungary$year)
summary(m1)
##
## Call:
## lm(formula = hungary$clutch_volume ~ hungary$LD_julian_day +
## I(hungary$LD_julian_day^2) + hungary$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.8858 -0.6897 0.5586 1.4634 4.0434
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.939e+01 3.071e+02 -0.259 0.796
## hungary$LD_julian_day 4.185e-02 9.665e-02 0.433 0.666
## I(hungary$LD_julian_day^2) -2.667e-04 3.654e-04 -0.730 0.467
## hungary$year 5.167e-02 1.542e-01 0.335 0.738
##
## Residual standard error: 2.644 on 177 degrees of freedom
## Multiple R-squared: 0.06544, Adjusted R-squared: 0.0496
## F-statistic: 4.131 on 3 and 177 DF, p-value: 0.00734
# dropping quadratic term
m2 <- lm(hungary$clutch_volume ~ hungary$LD_julian_day + hungary$year)
summary(m2)
##
## Call:
## lm(formula = hungary$clutch_volume ~ hungary$LD_julian_day +
## hungary$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.1187 -0.6481 0.5419 1.4359 3.8105
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -86.314349 306.522662 -0.282 0.77858
## hungary$LD_julian_day -0.028397 0.008589 -3.306 0.00114 **
## hungary$year 0.057395 0.153792 0.373 0.70945
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.641 on 178 degrees of freedom
## Multiple R-squared: 0.06263, Adjusted R-squared: 0.05209
## F-statistic: 5.946 on 2 and 178 DF, p-value: 0.003164
# dropping year
m3 <- lm(hungary$clutch_volume ~ hungary$LD_julian_day)
summary(m3)
##
## Call:
## lm(formula = hungary$clutch_volume ~ hungary$LD_julian_day)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.0265 -0.6670 0.5037 1.4546 3.9026
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 28.07803 1.11299 25.227 < 2e-16 ***
## hungary$LD_julian_day -0.02897 0.00843 -3.437 0.000732 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.634 on 179 degrees of freedom
## Multiple R-squared: 0.06189, Adjusted R-squared: 0.05665
## F-statistic: 11.81 on 1 and 179 DF, p-value: 0.0007325
# model selection using AIC
AIC (m1, m2, m3)
## df AIC
## m1 5 871.6095
## m2 4 870.1531
## m3 3 868.2947
# plot model diagnostics
par(mfrow = c(2,2))
plot(m3)
#### repeat modeling with outliers removed ####
# build a maximal model
or_m1 <- lm(outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day
+ I(outliers.rem$LD_julian_day^2) + outliers.rem$year)
summary(or_m1)
##
## Call:
## lm(formula = outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day +
## I(outliers.rem$LD_julian_day^2) + outliers.rem$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.1548 -0.6894 0.4479 1.4194 3.6932
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.441e+02 2.831e+02 -0.862 0.390
## outliers.rem$LD_julian_day -1.541e-02 8.921e-02 -0.173 0.863
## I(outliers.rem$LD_julian_day^2) -2.423e-05 3.378e-04 -0.072 0.943
## outliers.rem$year 1.360e-01 1.422e-01 0.957 0.340
##
## Residual standard error: 2.426 on 176 degrees of freedom
## Multiple R-squared: 0.05252, Adjusted R-squared: 0.03637
## F-statistic: 3.252 on 3 and 176 DF, p-value: 0.0231
# removing quadratic term
or_m2 <- lm(outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day + outliers.rem$year)
summary(or_m2)
##
## Call:
## lm(formula = outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day +
## outliers.rem$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.1688 -0.7079 0.4528 1.4296 3.7041
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.450e+02 2.821e+02 -0.868 0.38633
## outliers.rem$LD_julian_day -2.179e-02 7.947e-03 -2.742 0.00674 **
## outliers.rem$year 1.367e-01 1.415e-01 0.966 0.33555
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.419 on 177 degrees of freedom
## Multiple R-squared: 0.05249, Adjusted R-squared: 0.04179
## F-statistic: 4.903 on 2 and 177 DF, p-value: 0.008463
# removing year
or_m3 <- lm(outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day)
summary(or_m3)
##
## Call:
## lm(formula = outliers.rem$clutch_volume ~ outliers.rem$LD_julian_day)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.9099 -0.6667 0.4665 1.3842 3.5999
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.412802 1.028127 26.663 < 2e-16 ***
## outliers.rem$LD_julian_day -0.023243 0.007801 -2.979 0.00329 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.419 on 178 degrees of freedom
## Multiple R-squared: 0.0475, Adjusted R-squared: 0.04215
## F-statistic: 8.877 on 1 and 178 DF, p-value: 0.003291
# model selection using AIC
AIC (or_m1, or_m2, or_m3) # models comparable
## df AIC
## or_m1 5 835.8081
## or_m2 4 833.8134
## or_m3 3 832.7592
# plot model diagnostics
par(mfrow = c(2,2))
plot(or_m3)
# check if there are any missing values for clutch volume
table(is.na(galicia$clutch_volume))
##
## FALSE TRUE
## 439 50
# subset galicia dataset to remove missing values for clutch volume
galicia_clutch <- subset(galicia, galicia$clutch_volume != "NA")
# basic descriptive stats for clutch volume
dim(galicia_clutch)
## [1] 439 37
mean(galicia_clutch$clutch_volume)
## [1] 24.7808
sd(galicia_clutch$clutch_volume)
## [1] 1.535776
range(galicia_clutch$clutch_volume)
## [1] 19.82758 30.66049
# check for normality, zero inflation and outliers
hist(galicia_clutch$clutch_volume)
# subset covariates of interest to check for multicollinearity
covariates <- galicia_clutch[, c("clutch_volume", "LD_julian_day", "year")]
# plot the relations
pairs.panels(covariates) ##high correlation between some covariates
# visually inspect relationships by each year
# plot for clutch volume vs laying date
qplot(LD_julian_day, clutch_volume, data = galicia_clutch, geom = "point",
xlab = "Laying date (Julian days)", ylab = "Clutch volume (cm3)",
main = "Clutch volume vs Laying date") + theme_classic() +
facet_wrap(~year, ncol=4, nrow=4) + theme_bw()
# visually inspect relationship between clutch volume and year
boxplot(galicia_clutch$clutch_volume ~ galicia_clutch$year, xlab = "year",
ylab = "Clutch volume (cm3)", main = "Clutch volume vs year")
# modeling
# run a GAM to see if there is any curvature
M <- gam(galicia_clutch$clutch_volume ~ s(galicia_clutch$LD_julian_day))
plot(M)
# build a maximal model
m1 <- lm(galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day
+ I(galicia_clutch$LD_julian_day^2) + galicia_clutch$year)
summary(m1)
##
## Call:
## lm(formula = galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day +
## I(galicia_clutch$LD_julian_day^2) + galicia_clutch$year)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.703 -1.054 0.044 1.004 6.005
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.573e+01 2.937e+01 2.578 0.0103 *
## galicia_clutch$LD_julian_day 6.386e-02 2.577e-02 2.478 0.0136 *
## I(galicia_clutch$LD_julian_day^2) -2.227e-04 9.272e-05 -2.402 0.0167 *
## galicia_clutch$year -2.759e-02 1.464e-02 -1.884 0.0602 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.524 on 435 degrees of freedom
## Multiple R-squared: 0.02234, Adjusted R-squared: 0.0156
## F-statistic: 3.313 on 3 and 435 DF, p-value: 0.01998
# remove year
m2 <- lm(galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day
+ I(galicia_clutch$LD_julian_day^2))
summary(m2)
##
## Call:
## lm(formula = galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day +
## I(galicia_clutch$LD_julian_day^2))
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.9618 -1.0329 0.0743 1.0018 5.7066
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.049e+01 1.744e+00 11.746 <2e-16
## galicia_clutch$LD_julian_day 6.175e-02 2.582e-02 2.392 0.0172
## I(galicia_clutch$LD_julian_day^2) -2.133e-04 9.286e-05 -2.297 0.0221
##
## (Intercept) ***
## galicia_clutch$LD_julian_day *
## I(galicia_clutch$LD_julian_day^2) *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.528 on 436 degrees of freedom
## Multiple R-squared: 0.01436, Adjusted R-squared: 0.00984
## F-statistic: 3.176 on 2 and 436 DF, p-value: 0.04271
# remove quadratic term
m3 <- lm(galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day)
summary(m3)
##
## Call:
## lm(formula = galicia_clutch$clutch_volume ~ galicia_clutch$LD_julian_day)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.849 -1.128 0.088 1.023 5.845
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24.406482 0.370160 65.935 <2e-16 ***
## galicia_clutch$LD_julian_day 0.002748 0.002663 1.032 0.303
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.536 on 437 degrees of freedom
## Multiple R-squared: 0.00243, Adjusted R-squared: 0.0001468
## F-statistic: 1.064 on 1 and 437 DF, p-value: 0.3028
# model selection using AIC
AIC (m1, m2, m3)
## df AIC
## m1 5 1621.602
## m2 4 1623.170
## m3 3 1626.453
# plot model diagnostics
par(mfrow = c(2,2))
plot(m1)
# check if there are any missing values for clutch volume
table(is.na(cheetham$clutch_volume))
##
## FALSE TRUE
## 371 102
# subset cheetham dataset to remove missing values for clutch volume
cheetham_clutch <- subset(cheetham, cheetham$clutch_volume != "NA")
# basic descriptive stats for clutch volume
dim(cheetham_clutch)
## [1] 371 47
mean(cheetham_clutch$clutch_volume)
## [1] 14.66838
sd(cheetham_clutch$clutch_volume)
## [1] 1.027192
range(cheetham_clutch$clutch_volume)
## [1] 10.52962 17.69257
# check for normality, zero inflation and for outliers
hist(cheetham_clutch$clutch_volume)
# subset covariates of interest for multiocollinearity
covariates <- cheetham_clutch[, c("clutch_volume", "LD_julian_day", "season_numeric")]
# plot the relations
pairs.panels(covariates)
# visually inspect relationships by each breeding_season
# plot for clutch volume vs laying date
qplot(LD_julian_day, clutch_volume, data = cheetham_clutch, geom = "point",
xlab = "Laying date (Julian days)", ylab = "Clutch volume (cm3)",
main = "Clutch volume vs Laying date") + theme_classic() +
facet_wrap(~breeding_season, ncol=3, nrow=3) + theme_bw()
# visually inspect relationship between clutch volume and breeding_season
boxplot(cheetham_clutch$clutch_volume ~ cheetham_clutch$breeding_season,
xlab = "breeding_season", ylab = "Clutch volume (cm3)",
main = "Clutch volume vs breeding_season")
# modeling
# run a GAM to see if there is any curvature
M <- gam(cheetham_clutch$clutch_volume ~ s(cheetham_clutch$LD_julian_day))
plot(M) # doesn't seem to have curvature
# build a maximal model
m1 <- lm(cheetham_clutch$clutch_volume ~ cheetham_clutch$LD_julian_day
+ I(cheetham_clutch$LD_julian_day^2) + cheetham_clutch$season_numeric)
summary(m1)
##
## Call:
## lm(formula = cheetham_clutch$clutch_volume ~ cheetham_clutch$LD_julian_day +
## I(cheetham_clutch$LD_julian_day^2) + cheetham_clutch$season_numeric)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1491 -0.7054 -0.1085 0.6860 2.9407
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.426e+02 4.707e+01 3.030 0.00262
## cheetham_clutch$LD_julian_day -4.654e-03 5.977e-03 -0.779 0.43664
## I(cheetham_clutch$LD_julian_day^2) 8.651e-06 1.798e-05 0.481 0.63078
## cheetham_clutch$season_numeric -6.331e-04 2.343e-04 -2.702 0.00721
##
## (Intercept) **
## cheetham_clutch$LD_julian_day
## I(cheetham_clutch$LD_julian_day^2)
## cheetham_clutch$season_numeric **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.015 on 367 degrees of freedom
## Multiple R-squared: 0.03129, Adjusted R-squared: 0.02337
## F-statistic: 3.951 on 3 and 367 DF, p-value: 0.008555
# dropping quadratic term
m2 <- lm(cheetham_clutch$clutch_volume ~ cheetham_clutch$LD_julian_day
+ cheetham_clutch$season_numeric)
summary(m2)
##
## Call:
## lm(formula = cheetham_clutch$clutch_volume ~ cheetham_clutch$LD_julian_day +
## cheetham_clutch$season_numeric)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1714 -0.6879 -0.1057 0.6821 2.9625
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.473e+02 4.602e+01 3.200 0.00149 **
## cheetham_clutch$LD_julian_day -1.821e-03 1.007e-03 -1.808 0.07144 .
## cheetham_clutch$season_numeric -6.572e-04 2.286e-04 -2.874 0.00428 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.014 on 368 degrees of freedom
## Multiple R-squared: 0.03068, Adjusted R-squared: 0.02541
## F-statistic: 5.823 on 2 and 368 DF, p-value: 0.003237
# dropping laying date
m3 <- lm(cheetham_clutch$clutch_volume ~ cheetham_clutch$season_numeric)
summary(m3)
##
## Call:
## lm(formula = cheetham_clutch$clutch_volume ~ cheetham_clutch$season_numeric)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.1962 -0.6862 -0.1109 0.6462 3.0643
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.479e+02 4.616e+01 3.203 0.00148 **
## cheetham_clutch$season_numeric -6.617e-04 2.293e-04 -2.886 0.00414 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.017 on 369 degrees of freedom
## Multiple R-squared: 0.02207, Adjusted R-squared: 0.01942
## F-statistic: 8.327 on 1 and 369 DF, p-value: 0.004135
# model selection using AIC
AIC (m1, m2, m3)
## df AIC
## m1 5 1069.965
## m2 4 1068.199
## m3 3 1069.479
# plot model diagnostics
par(mfrow = c(2,2))
plot(m2)
# load data file for Hungary
hungary <- read.csv("../Data/Hungary_numofnests.csv")
# load data file for Galicia
galicia <- read.csv("../Data/Galicia_numofnests.csv")
# load data file for Samouco
samouco <- read.csv("../Data/Samouco_numofnests.csv")
# load data file for Cheetham
cheetham <- read.csv("../Data/Cheetham_numofnests.csv")
# convert year to factor for all population datasets
hungary$week <- as.factor(hungary$week)
galicia$week <- as.factor(galicia$week)
samouco$week <- as.factor(samouco$week)
cheetham$week <- as.factor(cheetham$week)
# plot for Hungary
p1 = ggplot(hungary, aes(x = week, y = num.of.nests)) + xlab("Week of the year") +
ylab("Number of nests") + ggtitle("Hungary (n = 180)") +
geom_boxplot(fill = "red", alpha = 5/10) + theme_bw() +
theme(axis.text.x= element_text(angle = 90))
# plot for Galicia
p2 = ggplot(galicia, aes(x = week, y = num.of.nests)) + xlab("Week of the year") +
ylab("Number of nests") + ggtitle("Galicia (n = 489)") +
geom_boxplot(fill = "green", alpha = 5/10) + theme_bw() +
theme(axis.text.x= element_text(angle = 90))
# plot for Samouco
p3 = ggplot(samouco, aes(x = week, y = num.of.nests)) + xlab("Week of the year") +
ylab("Number of nests") + ggtitle("Samouco (n = 663)") +
geom_boxplot(fill = "blue", alpha = 5/10) + theme_bw() +
theme(axis.text.x= element_text(angle = 90))
# plot for Cheetham
p4 = ggplot(cheetham, aes(x = week, y = num.of.nests)) + xlab("Week of the year") +
ylab("Number of nests") + ggtitle("Cheetham (n = 473)") +
geom_boxplot(fill = "yellow", alpha = 5/10) + theme_bw() +
theme(axis.text.x= element_text(angle = 90))
# arrange the plots together
grid.arrange(p1, p2, p3, p4, nrow = 2, ncol = 2)
# add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 6) + 0.1)
# plot moving averages for pre-laying period temperature against number of week
plot(nests.per.week$temp.6.week ~ nests.per.week$week, main="",
ylab="", xlab="", pch = 18, col = "red", xaxt="n", cex.main = 1, axes = FALSE)
# customize y axis
axis(2,col="red", col.axis= "red", las=1) ## las=1 makes horizontal labels
# add y axis label
mtext("Average temperature (Degree celsius)",side=2,col = "red", line=2.5)
# draw a box around the plot
box()
# Allow a second plot on the same graph
par(new=TRUE)
# Plot the second plot and put axis scale on right
# plot moving averages for pre-laying period precipitation against number of week
plot(nests.per.week$prec.6.week ~ nests.per.week$week, pch=18, xlab="", ylab="",
axes=FALSE, col="blue", cex.main = 1)
# customize y axis
axis(4, col="blue",col.axis="blue",las=1)
# add y axis label
mtext("Average precipitation (mm)",side=4,col="blue",line=2.5)
# Draw the time axis
axis(1,pretty(range(nests.per.week$week),10))
# add x axis label
mtext("Week of the year",side=1,col="black",line=2.5)